使用类构造函数C ++实现Seg Fault

时间:2013-11-11 01:11:30

标签: c++ class constructor segmentation-fault

我是新手,并且对构造函数有很多困难。我有两个用于业务类的构造函数,每当我尝试创建业务对象或对业务对象执行任何操作时,我立即就会出现Seg Fault。业务类与另一个名为Customer的类进行交互。如果有人能提供任何帮助,我将非常感激。

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 ++;
  }    
}

1 个答案:

答案 0 :(得分:1)

void Business::addCustomer(Customer newCustomer)
{
  custInBus[numOfCustom] = newCustomer;
  //use numOfCustom instead of numOfCustom-1
  numOfCustom++;
}