Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用类和构造函数并创建数组大小为3的对象名_C++_Arrays_Class_Oop_Constructor - Fatal编程技术网

C++ 使用类和构造函数并创建数组大小为3的对象名

C++ 使用类和构造函数并创建数组大小为3的对象名,c++,arrays,class,oop,constructor,C++,Arrays,Class,Oop,Constructor,我正在尝试创建一个包含三个数组的主对象。当我运行它时,它会显示所需的输出,但它会给出一个错误,之后的任何代码都不会运行。Grocery.exe 0x775 FC41F未处理异常:微软C++异常:STD::BADYOLL在内存位置0x00 36E590.< /P> class GroceryItem { public: GroceryItem();//default constructor GroceryItem(string, double, int, int);

我正在尝试创建一个包含三个数组的主对象。当我运行它时,它会显示所需的输出,但它会给出一个错误,之后的任何代码都不会运行。Grocery.exe 0x775 FC41F未处理异常:微软C++异常:STD::BADYOLL在内存位置0x00 36E590.< /P>
    class GroceryItem {
    public:
    GroceryItem();//default constructor
    GroceryItem(string, double, int, int); // four argument constructor
    void set_item_name(string); // Assigns a value to the data memeber item_name
    void set_item_price(double); //Assigns a value to the data member item_price
    void set_qty_on_hand(int);// Assigns a value to the data member quantity_on_hand.
    void set_qty_purchased(int); // Sets qty_purchased to zero before a customer begins shopping.
    string get_item_name(); // Returns the value of the data memebr item_name.
    double get_item_price(); // Returns the value of the data member item_price
    int get_qty_on_hand(); // Returns the value of the data member quantity_on_hand.
    int get_qty_purchased(); // Retruns the value of the data memebr qty_purchased.
    private:
            string item_name;
            double item_price;
            int quantity_on_hand;
            int quantity_purchased;
     };

   GroceryItem::GroceryItem(string name, double price, int hand, int purchased){
   set_item_name(name);
   set_item_price(price);
   set_qty_on_hand(hand);
   set_qty_purchased(purchased);
   }
   void GroceryItem::set_item_name(string name){
   item_name = name;
   }
   string GroceryItem::get_item_name(){
   return item_name;
   }
   void GroceryItem::set_item_price(double price){
   item_price = price;
   }
   double GroceryItem::get_item_price(){
   return item_price;
   }
  void GroceryItem::set_qty_on_hand(int hand){
  quantity_on_hand = hand;
  }
  int GroceryItem::get_qty_on_hand(){
  return quantity_on_hand;
  }
  void GroceryItem::set_qty_purchased(int purchased){
    if (purchased > 0)
    quantity_purchased = purchased;
   if (purchased <= 0)
   {
    quantity_purchased = 0;
    cout <<"\n cart cannot be negative, it will be set to 0. \n";
    }
   }
   int GroceryItem::get_qty_purchased(){
    return quantity_purchased;
   }



int main(){
int input;
cout <<"Welcome to KMART\n" << "\nHappy Shopping" << endl;
GroceryItem Kmart("Hello", 1234, 1234, 1);
GroceryItem Kmart2("My", 1234, 1234, 1);
GroceryItem Kmart3[SIZE] = {
    { "John", 1234, 1234, 0 },
    { "Mary", 1234, 1234, 0 },
    { "Kevin", 1234, 1234, 0 }

};
//Kmart("jjj", 12, 123, 1);
for (int i = 0; i < 10; i++){
cout << Kmart3[i].get_item_name() << "\t" << Kmart3[i].get_item_price() << "\t" <<                   Kmart3[i].get_qty_on_hand() << "\t" << Kmart3[i].get_qty_purchased() << endl;
}
cout << "This is fromt eh four argument constructor" << endl;
cout<<Kmart.get_item_name()<<"\t"<<Kmart.get_item_price()<<"\t"<<Kmart.get_qty_on_hand()<<"\t"<<Kmart.get_qty_purchased()<<endl;
cout << Kmart2.get_item_name() << "\t" << Kmart2.get_item_price() << "\t" << Kmart2.get_qty_on_hand() << "\t" << Kmart2.get_qty_purchased() << endl;

}

您没有与我们分享尺寸值;然而,我猜你可能不到10。如果是这样的话,那么当您在Kmart3中循环时,它会崩溃

进行此更改应至少在我的测试中停止崩溃:

for (int i = 0; i < SIZE; i++)
{
    if (Kmart3[i].get_item_name().length() > 0)
        cout << Kmart3[i].get_item_name().c_str() << "\t" << Kmart3[i].get_item_price() << "\t" << Kmart3[i].get_qty_on_hand() << "\t" << Kmart3[i].get_qty_purchased() << endl;
}

当然,在代码运行之前,您还需要解决一些其他问题,我认为您的意思是让它运行;但是,这应该可以停止崩溃。

错误来自Kmart3输出。感谢您的回复,我实际上是以10的大小进行的,但我更改了一些内容,将大小改为3。当我用10号而不是3号时,导致了崩溃。