c++中使用复制构造函数的逻辑错误
logical error using copy constructor in c++
在下面的程序中,我的预期输出是显示客户对象感兴趣的项目列表,但它显示的是空值。调用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; }
相关文章:
- "error: no matching function for call to"构造函数错误
- 为什么类中的ostringstream类型的成员会导致";调用隐含删除复制构造函数";错误
- 模板,函数使用错误的构造函数来复制我的对象
- C++ OpenCV 卡尔曼滤波器构造函数错误
- 不命名构造函数和析构函数上的类型错误
- 处理简单 cpp 类构造函数中的错误
- 类和构造函数中的函数根本不起作用,并且不返回任何错误
- 链接器错误:无法解析构造函数
- 当我尝试通过构造函数分配 char 数组时出现错误
- 没有参数的构造函数给我错误
- 使用复制构造函数的程序输出错误
- 默认构造函数上的分段错误
- 错误: 无效使用非静态数据成员"应用程序::应用程序构造函数"
- 为什么定义复制构造函数会给我错误:无法将类型 'obj&' 的非常量左值引用绑定到类型为"obj"的右值?
- 带有 QWidget 的分段错误构造函数.Qt/C++
- C++错误构造函数参数
- 错误:构造函数未命名类型
- 错误:构造函数在此上下文中受到保护
- 没有匹配的函数错误C++构造函数
- 错误:构造函数的多个定义