简体   繁体   English

使用类构造函数C ++的段错误

[英]Seg Fault with Class Constructors C++

I am new to classes and am having a lot of difficulty with the constructors. 我是新手,在构造函数上遇到很多困难。 I have two constructors for a business class and whenever I attempt to create a business object or do anything with the business object I immediately Seg Fault. 我有两个用于业务类的构造函数,每当我尝试创建一个业务对象或对该业务对象执行任何操作时,我都会立即进行Seg Fault。 The business class interacts with an additional class called Customer. 业务类与另一个名为Customer的类进行交互。 If anyone could offer any help I would greatly appreciate it. 如果有人可以提供任何帮助,我将不胜感激。

Business.h 业务.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 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 ++;
  }    
}
void Business::addCustomer(Customer newCustomer)
{
  custInBus[numOfCustom] = newCustomer;
  //use numOfCustom instead of numOfCustom-1
  numOfCustom++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM