0

我是新来的类,在构造函数方面遇到了很多困难。我有两个用于业务类的构造函数,每当我尝试创建业务对象或对业务对象执行任何操作时,我都会立即 Seg Fault。业务类与称为 Customer 的附加类交互。如果有人可以提供任何帮助,我将不胜感激。

商务.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 

商务.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 ++;
  }    
}
4

1 回答 1

1
void Business::addCustomer(Customer newCustomer)
{
  custInBus[numOfCustom] = newCustomer;
  //use numOfCustom instead of numOfCustom-1
  numOfCustom++;
}
于 2013-11-11T01:16:45.687 回答