0
Я новичок в классах, и у меня много трудностей с конструкторами. У меня есть два конструктора для бизнес-класса, и всякий раз, когда я пытаюсь создать бизнес-объект или что-то делать с бизнес-объектом, я сразу же Seg Fault. Бизнес-класс взаимодействует с дополнительным классом Customer. Если бы кто-нибудь мог оказать какую-либо помощь, я бы очень признателен.Seg Fault с конструкторами классов C++
Business.h
#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
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 ++;
}
}
вы знаете, что ваш не-конструктор по умолчанию не устанавливается значение 'numOfItems' ли? – nhgrif
Будьте осторожны с вашим 'custInBus [numOfCustom-1]'. Убедитесь, что 'numOfCustom> = 1', а не 0. –
Вам нужно убедиться, что ваш конструктор не устанавливает numOfCustom> 10. –