C++ 类构造函数C+的Seg错误+;
我是新来的班级,和建设者在一起有很多困难。对于一个业务类,我有两个构造函数,每当我试图创建一个业务对象或对该业务对象执行任何操作时,我都会立即查找错误。业务类与另一个名为Customer的类交互。如果有人能提供任何帮助,我将不胜感激 Business.hC++ 类构造函数C+的Seg错误+;,c++,class,constructor,segmentation-fault,C++,Class,Constructor,Segmentation Fault,我是新来的班级,和建设者在一起有很多困难。对于一个业务类,我有两个构造函数,每当我试图创建一个业务对象或对该业务对象执行任何操作时,我都会立即查找错误。业务类与另一个名为Customer的类交互。如果有人能提供任何帮助,我将不胜感激 Business.h #ifndef BUSINESS_H #define BUSINESS_H #include <iostream> #include <string> #include "customer.h" using name
#ifndef BUSINESS_H
#define BUSINESS_H
#include <iostream>
#include <string>
#include "customer.h"
using namespace std;
class Business
{
public:
Business();
Business(string name, float cash);
void printData() const;
void addCustomer(Customer newCustomer);
void make_a_sale();
private:
string businessName;
float cashInReg;
string itemArray[10];
Customer custInBus[10];
short numOfItems;
short numOfCustom;
};
#endif
#如果没有业务#
#定义业务
#包括
#包括
#包括“customer.h”
使用名称空间std;
阶级商业
{
公众:
商业();
业务(字符串名称、浮动现金);
void printData()常量;
无效添加客户(客户新客户);
使销售无效();
私人:
字符串businessName;
浮动现金;
字符串项数组[10];
客户custInBus[10];
短项;
短裙习俗;
};
#恩迪夫
Business.cpp
#include "business.h"
#include <iostream>
#include <cstdlib>
using namespace std;
Business::Business(): businessName("Business"), cashInReg(0), numOfItems(0),
numOfCustom(0) {}
Business::Business(string name, float cash) : businessName(name),
cashInReg(cash), numOfCustom(0) {}
void Business::printData() const
{
cout << businessName <<endl;
for (int i=0; i<numOfCustom; i++)
{
cout << "\t Customer Name: " << custInBus[i].getName() <<endl;
}
for (int i=0; i<numOfItems; i++)
{
cout << "\t Item list: " <<itemArray[i] <<endl;
}
}
void Business::addCustomer(Customer newCustomer)
{
custInBus[numOfCustom-1] = newCustomer;
numOfCustom++;
}
void Business::make_a_sale()
{
int randomItem;
int currCustomer=0;
while (currCustomer < numOfCustom)
{
randomItem = rand() %tempItems;
custInBus[currCustomer].purchase(tempArray[randomItem]);
currCustomer ++;
}
}
#包括“business.h”
#包括
#包括
使用名称空间std;
Business::Business():businessName(“Business”)、cashInReg(0)、numOfItems(0),
numOfCustom(0){}
Business::Business(字符串名称,浮动现金):businessName(名称),
cashInReg(cash),numOfCustom(0){}
void Business::printData()常量
{
难道你不知道你的非默认构造函数没有为numOfItems
设置值吗?小心你的custInBus[numOfCustom-1]
。确保numOfCustom>=1
而不是0。你需要验证你的构造函数没有设置numOfCustom>10。
void Business::addCustomer(Customer newCustomer)
{
custInBus[numOfCustom] = newCustomer;
//use numOfCustom instead of numOfCustom-1
numOfCustom++;
}