- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有使用动态分配内存的作业。我的教授给了我一些指示。使用它们,我编写了以下代码。我随机收到错误消息。有时错误出现在复制执行之前。有时它会复制一个对象而不会复制下一个对象。我不明白我做错了什么。
默认构造函数
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 == ©_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;
}
}
}
}
这里是错误
最佳答案
您的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 != ©_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/
我正在使用 node.js 和 mocha 单元测试,并且希望能够通过 npm 运行测试命令。当我在测试文件夹中运行 Mocha 测试时,测试运行成功。但是,当我运行 npm test 时,测试给出了
我的文本区域中有这些标签 ..... 我正在尝试使用 replaceAll() String 方法替换它们 text.replaceAll("", ""); text.replaceAll("", "
早上好,我是 ZXing 的新手,当我运行我的应用程序时出现以下错误: 异常Ljava/lang/NoClassDefFoundError;初始化 ICOM/google/zxing/client/a
我正在制作一些哈希函数。 它的源代码是... #include #include #include int m_hash(char *input, size_t in_length, char
我正在尝试使用 Spritekit 在 Swift 中编写游戏。目的是带着他的角色迎面而来的矩形逃跑。现在我在 SKPhysicsContactDelegate (didBegin ()) 方法中犯了
我正在尝试创建一个用于导入 CSV 文件的按钮,但出现此错误: actionPerformed(java.awt.event.ActionEvent) in cannot implement
请看下面的代码 public List getNames() { List names = new ArrayList(); try { createConnection(); Sta
我正在尝试添加一个事件以在“dealsArchive”表中创建一个条目,然后从“deals”表中删除该条目。它需要在特定时间执行。 这是我正在尝试使用的: DELIMITER $$ CREATE EV
我试图将两个存储过程的表结果存储到 phpmyadmin 例程窗口中的单个表中,这给了我 mariadb 语法错误。单独调用存储过程给出了结果。 存储过程代码 BEGIN CREATE TABLE t
我想在 videoview 中加载视频之前有一个进度条。但是我收到以下错误。我还添加了所有必要的导入。 我在 ANDROID 中使用 AIDE 这是我的代码 public class MainActi
我已经使用了 AsyncTask,但我不明白为什么在我的设备 (OS 4.0) 上测试时仍然出现错误。我的 apk 构建于 2.3.3 中。我想我把代码弄错了,但我不知道我的错误在哪里。任何人都请帮助
我在测试 friend 网站的安全性时,通过在 URL 末尾添加 ' 发现了 SQL 注入(inject)漏洞该网站是用zend框架构建的我遇到的问题是 MySQL -- 中的注释语法不起作用,因此页
我正在尝试使用堆栈溢出答案之一的交互式信息窗口。 链接如下: interactive infowindow 但是我在代码中使用 getMap() 时遇到错误。虽然我尝试使用 getMapAsync 但
当我编译以下代码时出现错误: The method addMouseListener(Player) is undefined for the type Player 代码: import java.
我是 Android 开发的初学者。我正在开发一个接收 MySql 数据然后将其保存在 SQLite 中的应用程序。 我将 Json 用于同步状态,以便我可以将未同步数据的数量显示为要同步的待处理数据
(这里是Hello world级别的自动化测试人员) 我正在尝试下载一个文件并将其重命名以便于查找。我收到一个错误....这是代码 @Test public void allDownload(
我只是在写另一个程序。并使用: while (cin) words.push_back(s); words是string的vector,s是string。 我的 RAM 使用量在 4 或 5
我是 AngularJS 的新手,我遇到了一个问题。我有一个带有提交按钮的页面,当我单击提交模式时必须打开并且来自 URL 的数据必须存在于模式中。现在,模式打开但它是空的并且没有从 URL 获取数据
我正在尝试读取一个文件(它可以包含任意数量的随机数字,但不会超过 500 个)并将其放入一个数组中。 稍后我将需要使用数组来做很多事情。 但到目前为止,这一小段代码给了我 no match for o
有些人在使用 make 命令进行编译时遇到了问题,所以我想我应该在这里尝试一下,我已经在以下操作系统的 ubuntu 32 位和挤压 64 位上尝试过 我克隆了 git 项目 https://gith
我是一名优秀的程序员,十分优秀!