c++中使用复制构造函数的逻辑错误

logical error using copy constructor in c++

本文关键字:错误 构造函数 复制 c++      更新时间:2023-10-16

在下面的程序中,我的预期输出是显示客户对象感兴趣的项目列表,但它显示的是空值。调用showitem()时不打印任何值。请帮我纠正我代码中的逻辑错误。

class item
{
    int itemno;
    string name;
public:
    item()
    {
        itemno=0;
    }
    item(int r,string n)
    {
        itemno = r;
        name = n;
    }
    void showitem()
    {
        cout<<"Item no is:"<<itemno<<"n";
        cout<<"Name is :"<<name<<"n";
    }
};
class customer
{
public:
    int customerno;
    string customername;
    string address;
    int totalitem;
    item *iteminterested;
    customer()
    {
        iteminterested=0;
        totalitem=0;
    }
    customer(customer &custref)
    {
        customerno=custref.customerno;
        customername=custref.customername;
        address=custref.address;
        iteminterested=new item[custref.totalitem];
        for(int i=0;i<custref.totalitem;++i)
        {
            iteminterested[i]=custref.iteminterested[i];
        }
        totalitem=custref.totalitem;
    }
    customer(int no,string cusname,string add,item *temp,int total)
    {
        customerno=no;
        customername=cusname;
        address=add;
        iteminterested=new item[total];
        for(int i=0;i<totalitem;++i)
        {
            iteminterested[i]=temp[i];
        }
        totalitem=total;
    }
    void showcustomer()
    {
        cout<<"customer name is"<<customername<<"n";
        cout<<"customer number is"<<customerno<<"n";
        cout<<"customer address is"<<address<<"n";
        cout<<"customer " <<customername <<"intersted in n";
        for(int k=0;k<totalitem;k++)
        {
            iteminterested[k].showitem();
        }
    }
};
int main()
{
    customer steve;
    item itemarray[]={item(3,"sandwiches"),item(4,"paperbags"),item(5,"biscuits"),item(6,"coke"),item(10,"biscuits"),item(9,"pen"),item(1,"pencil"),item(2,"eraser")};
    steve=customer(2,"steve","aus",itemarray,5);
    steve.showcustomer();
    customer mark(steve);
    mark.showcustomer();
    mark.showcustomer();
    steve.showcustomer();
    return 0;
}

在客户构造函数中,必须在分配项目之前设置totalitem。它没有为循环初始化。或者在循环中使用total

#include <iostream>
#include <string>
using namespace std;
class Item {
  int itemno;
  string name;
public:
  Item()
    : itemno(0) { }
  Item(int r, string n)
    : itemno(r),
      name(n) { }
  void showitem() const {
    cout << "Item no is: " << itemno << "n";
    cout << "Name is: " << name << "n"; } };

class Customer {
public:
  int customerno;
  string customername;
  string address;
  int totalitem;
  Item* iteminterested;
  Customer()
    :  totalitem(0),
       iteminterested(0) { }
  Customer(const Customer& custref)
    : customerno(custref.customerno),
      customername(custref.customername),
      address(custref.address),
      totalitem(custref.totalitem) {
    iteminterested = new Item[totalitem];
    for (int i = 0; i < totalitem; ++i) {
      iteminterested[i] = custref.iteminterested[i]; } }
  Customer(int no, string cusname, string add, Item* temp, int total)
    : customerno(no),
      customername(cusname),
      address(add),
      totalitem(total) {
    iteminterested = new Item[total];
    for (int i = 0; i < totalitem; ++i) {
      iteminterested[i] = temp[i]; } }
  void showcustomer() const {
    cout << "customer name is: " << customername << "n";
    cout << "customer number is: " << customerno << "n";
    cout << "customer address is: " << address << "n";
    cout << "customer " << customername << " intersted in n";
    for (int k = 0; k < totalitem; ++k) {
      iteminterested[k].showitem(); } } };
int main() {
  Customer steve;
  Item itemarray[] = {
    Item(3, "sandwiches"),
    Item(4, "paperbags"),
    Item(5, "biscuits"),
    Item(6, "coke"),
    Item(10, "biscuits"),
    Item(9, "pen"),
    Item(1, "pencil"),
    Item(2, "eraser") };
  steve = Customer(2, "steve", "aus", itemarray, 5);
  steve.showcustomer();
  Customer mark(steve);
  mark.showcustomer();
  mark.showcustomer();
  steve.showcustomer();
  return 0; }