gpt4 book ai didi

c++ - 动态分配内存,拷贝构造函数出错

转载 作者:行者123 更新时间:2023-11-30 05:27:34 29 4
gpt4 key购买 nike

我有使用动态分配内存的作业。我的教授给了我一些指示。使用它们,我编写了以下代码。我随机收到错误消息。有时错误出现在复制执行之前。有时它会复制一个对象而不会复制下一个对象。我不明白我做错了什么。

默认构造函数

GroceryItem::GroceryItem()
{
item_name = new char[strlen("") + 1];
strcpy(item_name, "");
item_price = 0;
qty_on_hand = 0;
qty_purchased = 0;
};

下面的函数是我用来复制两个对象的复制构造函数:

GroceryItem::GroceryItem(const GroceryItem& Grocery_in)
{
item_name = new char[strlen(Grocery_in.item_name) + 1];
strcpy(item_name, Grocery_in.item_name);
item_price = Grocery_in.item_price;
qty_on_hand = Grocery_in.qty_on_hand;
qty_purchased = Grocery_in.qty_purchased;
}
;

下面是赋值运算符

GroceryItem& GroceryItem::operator=(GroceryItem& copy_item)
{
if (this == &copy_item)
return *this;
else
{
delete[] item_name;
item_name = new char[strlen(copy_item.item_name)+1];
strcpy(item_name, copy_item.item_name);
item_price = copy_item.item_price;
qty_on_hand = copy_item.qty_on_hand;
qty_purchased = copy_item.qty_purchased;
return *this ; // They are the same
}
}

当我尝试复制到 temp2 时从下面的函数中调用:

void sort_items(GroceryItem ini_customer_GroceryItem[], int number)
{
int j = 0, k = 0;
GroceryItem temp2;

for (j = 0; j < number - 1; j++) // n-1 passes
{
for (k = number - 1; j < k; k--) // each pass runs one fewer than the preceding one
{
if (ini_customer_GroceryItem[k - 1] > ini_customer_GroceryItem[k])
{
temp2 = ini_customer_GroceryItem[k - 1];
ini_customer_GroceryItem[k - 1] = ini_customer_GroceryItem[k];
ini_customer_GroceryItem[k] = temp2;
}
}
}
}

这里是错误

image

最佳答案

您的sort_items() 函数应该使用std::swap()算法而不是手动复制对象:

/*
temp2 = ini_customer_GroceryItem[k - 1];
ini_customer_GroceryItem[k - 1] = ini_customer_GroceryItem[k];
ini_customer_GroceryItem[k] = temp2;
*/
std::swap(ini_customer_GroceryItem[k - 1], ini_customer_GroceryItem[k]);

无论哪种方式,您都没有实现复制构造函数,只实现了一个复制赋值运算符(并且copy_item 应该是const 在你的实现中)。参见 Rule of Three .您需要实现一个适当的复制构造函数:

GroceryItem::GroceryItem(const GroceryItem& source_item)
{
item_name = new char[strlen(source_item.item_name)+1];
strcpy(item_name, source_item.item_name);
item_price = source_item.item_price;
qty_on_hand = source_item.qty_on_hand;
qty_purchased = source_item.qty_purchased;
}

然后您可以使用复制构造函数实现您的复制赋值运算符:

GroceryItem& GroceryItem::operator=(const GroceryItem& copy_item)
{
if (this != &copy_item)
{
GroceryItem temp(copy_item);
std::swap(temp, *this);
}
return *this;
}

可以简化为:

GroceryItem& GroceryItem::operator=(GroceryItem copy_item)
{
std::swap(copy_item, *this);
return *this;
}

当然,如果您还没有实现析构函数,请不要忘记析构函数:

GroceryItem::~GroceryItem()
{
delete[] item_name;
}

当然还有一个 operator>(),因为 sort_items() 需要一个。

现在,综上所述,如果将 item_name 成员更改为 std::string 而不是 char*,您根本不需要手动实现析构函数、复制构造函数或复制赋值运算符(只需使用默认构造函数对数字成员进行零初始化)。编译器默认生成的析构函数、复制构造函数和复制赋值运算符的实现足以为您管理所有数据成员:

class GroceryItem
{
public:
std::string item_name;
float item_price;
int qty_on_hand;
int qty_purchased;

GroceryItem();

bool operator > (const GroceryItem& item) const;
};

GroceryItem::GroceryItem()
{
item_price = 0.0f;
qty_on_hand = 0;
qty_purchased = 0;
};

bool GroceryItem::operator > (const GroceryItem& item) const
{
return ...;
}

void sort_items(GroceryItem ini_customer_GroceryItem[], int number)
{
int j = 0, k = 0;
//GroceryItem temp2;

for (j = 0; j < number - 1; j++) // n-1 passes
{
for (k = number - 1; j < k; k--) // each pass runs one fewer than the preceding one
{
if (ini_customer_GroceryItem[k - 1] > ini_customer_GroceryItem[k])
{
/*
temp2 = ini_customer_GroceryItem[k - 1];
ini_customer_GroceryItem[k - 1] = ini_customer_GroceryItem[k];
ini_customer_GroceryItem[k] = temp2;
*/
std::swap(ini_customer_GroceryItem[k - 1], ini_customer_GroceryItem[k]);
}
}
}
}

关于c++ - 动态分配内存,拷贝构造函数出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37221432/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com