C++:使用重载的复合赋值运算符时出现运行时错误
C++: Runtime error when using overloaded compound assignment operators
我有一个使用 class 动态分配数组的程序。我重载了对 class 中的对象执行操作的运算符。
当我测试这个程序时,重载的 += 工作,但是 -= 不工作。 当试图 运行 重载的 -= 和我收到以下 运行 时间错误:
malloc: * error for object 0x7fd388500000: pointer being freed was not >allocated
* set a breakpoint in malloc_error_break to debug
在私有成员变量中,我这样声明数组:
double* array_d;
然后我在重载的构造函数中动态分配数组:
Students::Students(int classLists)
{
classL = classLists;
array_d = new double[classL];
}
我将以下两个重载构造函数定义为学生的朋友 class:
friend Student operator+= (const Student&, const Student&);
friend Student operator-= (const Student&, const Student&);
这些定义如下:
Student operator+= (const Student& stu1, const Student& stu2)
{
if (stu1.getClassL() >= stu2.getClassL())
{
for (int count = 0; count < stu.getClassL(); count++)
stu1.array_d[count] += stu2.array_d[count];
return (stu1);
}
else if (stu1.getClassL() < stu2.getClassL())
{
for (int count = 0; count < stu1.getClassL(); count++)
stu1.array_d[count] += stu2.array_d[count];
return (stu1);
}
}
Student operator-= (const Student& stu1, const Student& stu2)
{
if (stu1.getClassL() >= stu2.getClassL())
{
for (int count = 0; count < stu2.getClassL(); count++)
stu1.array_d[count] -= stu2.array_d[count];
return (stu1);
}
else if (stu1.getClassL() < stu2.getClassL())
{
for (int count = 0; count < stu1.getClassL(); count++)
stu1.array_d[count] -= stu2.array_d[count];
return (stu1);
}
}
基本上这里发生的事情是我正在比较两个对象,数组的大小基于 classL 而不同。 getClassL()
函数很简单:int Student::getClassL() const {return classLists;}
如果你想知道,我已经以这种方式超载了三巨头:
1.析构函数: Student::~Student() {delete [] array_d;}
2。复制构造函数:
Student::Student(const Student &student)
{
classLists = student.classLists;
array_d = student.array_d;
}
3。赋值运算符:
Student &Student::operator=(const Student &student)
{
classLists = student.classLists;
array_d = student.array_d;
}
奇怪的是 += 有效但 -= 无效,因为它们实际上是相同的。我怀疑我的问题出在我的动态内存分配上,但我不确定并正在寻求专家建议。
你的问题更多的是关于为什么 C++11/C++14 是比以前的版本更好的 C++,以及为什么应该这样使用它的研究。
首先,正如评论所建议的那样,运算符 +=
和 -=
应该 return 是参考,而不是副本。如果它是成员函数运算符,它将 return *this
,但在您的情况下不是。只需将 return 类型更改为 Student &
.
崩溃的原因(也包含在注释中)是当复制构造函数或赋值执行时,array_d 的所有权由两个 'Student' 对象承担。当第二个被销毁时,它会尝试delete [] array_d
,它已经被第一个删除了。
赋值运算符显示return什么都没有。它应该 return *this;
评论指出您需要创建一个新数组并将每个元素从源复制到目标,因此有两个单独的数组。
然而,这是说明为什么 STL 如此有价值的关键时刻。如果您改用 std::vector<double>
,向量的赋值运算符会为您复制所有元素。
在两个运算符函数(+=
和 -=
)中,“else if
”子句没有任何价值。逻辑上,如果stu1.getClassL()
不是>= stu2.getClassL()
,那只能是<
,所以节省时间,去掉else if子句,以及包含的大括号。
给你的建议是使用 std::vector
并且让你自己不必实现赋值运算符、复制构造函数和析构函数。
不过话说回来,代码中有一些错误,更不用说复制构造函数了。
首先,复制构造函数应该分配一个全新的数组,并将传入值中的值从数组复制到新数组。这是 Student
class 的简化版本,只有两个成员——一个 double *
和一个表示数组中项目数的整数。
class Student
{
int num;
double *array_d;
public:
Student(const Student &student);
Student& operator=(const Student &student);
~Student() { delete [] array_d; }
Student() : array_d(0), num(0) {}
};
复制构造函数看起来像这样:
Student::Student(const Student &student) :
array_d(new double[student.num]), num(student.num)
{
for (int i = 0; i < num; ++i )
array_d[i] = student.array_d[i];
}
一旦有了这个,赋值运算符就很简单了 copy / swap:
Student& Student::operator=(const Student &student)
{
Student temp = student;
std::swap(d_array, temp.d_array);
std::swap(num, temp.num);
return *this;
}
基本上上面所做的就是制作传入对象的临时副本,将临时对象的内部结构与当前对象交换,然后临时对象与旧内部结构一起消失。除非 Student
的复制构造函数和析构函数正常工作(现在应该是这样),否则所有这一切都是不可能的。
接下来要考虑的是您对运算符 +=
和 -=
的整个想法。大多数程序员期望使用它的方式是在这种情况下:
Student a;
Student b;
// assume a and b are initialized and have data...
a += b;
如果您以不同的形式使用 +=
或 -=
,它会变得不直观而且很奇怪。因此,这些函数应该采用单个参数,而不是两个参数,并且 return 当前对象(它是您正在更改的当前对象)。
因此这些函数不应该是友元函数,而是 class Student
:
的成员函数
class Student
{
int num;
double *array_d;
public:
Student(const Student &student);
Student& operator=(const Student &student);
~Student() { delete [] array_d; }
Student() : array_d(0), num(0) {}
Student& operator += (const Student& rhs);
Student& operator -= (const Student& rhs);
};
然后 +=
的实现看起来像这样:
#include <algorithm>
//...
Student& operator+= (const Student& stu1)
{
int num_to_add = std::min(num, stu1.num);
for (int count = 0; count < num_to_add; ++count)
array_d[count] += stu1.array_d[count];
return *this;
}
同样,-=
看起来像上面那样。请注意使用 std::min
来确定要添加的数量,而不是使用 if / else
.
的原始代码
我有一个使用 class 动态分配数组的程序。我重载了对 class 中的对象执行操作的运算符。
当我测试这个程序时,重载的 += 工作,但是 -= 不工作。 当试图 运行 重载的 -= 和我收到以下 运行 时间错误:
malloc: * error for object 0x7fd388500000: pointer being freed was not >allocated * set a breakpoint in malloc_error_break to debug
在私有成员变量中,我这样声明数组:
double* array_d;
然后我在重载的构造函数中动态分配数组:
Students::Students(int classLists)
{
classL = classLists;
array_d = new double[classL];
}
我将以下两个重载构造函数定义为学生的朋友 class:
friend Student operator+= (const Student&, const Student&);
friend Student operator-= (const Student&, const Student&);
这些定义如下:
Student operator+= (const Student& stu1, const Student& stu2)
{
if (stu1.getClassL() >= stu2.getClassL())
{
for (int count = 0; count < stu.getClassL(); count++)
stu1.array_d[count] += stu2.array_d[count];
return (stu1);
}
else if (stu1.getClassL() < stu2.getClassL())
{
for (int count = 0; count < stu1.getClassL(); count++)
stu1.array_d[count] += stu2.array_d[count];
return (stu1);
}
}
Student operator-= (const Student& stu1, const Student& stu2)
{
if (stu1.getClassL() >= stu2.getClassL())
{
for (int count = 0; count < stu2.getClassL(); count++)
stu1.array_d[count] -= stu2.array_d[count];
return (stu1);
}
else if (stu1.getClassL() < stu2.getClassL())
{
for (int count = 0; count < stu1.getClassL(); count++)
stu1.array_d[count] -= stu2.array_d[count];
return (stu1);
}
}
基本上这里发生的事情是我正在比较两个对象,数组的大小基于 classL 而不同。 getClassL()
函数很简单:int Student::getClassL() const {return classLists;}
如果你想知道,我已经以这种方式超载了三巨头:
1.析构函数: Student::~Student() {delete [] array_d;}
2。复制构造函数:
Student::Student(const Student &student)
{
classLists = student.classLists;
array_d = student.array_d;
}
3。赋值运算符:
Student &Student::operator=(const Student &student)
{
classLists = student.classLists;
array_d = student.array_d;
}
奇怪的是 += 有效但 -= 无效,因为它们实际上是相同的。我怀疑我的问题出在我的动态内存分配上,但我不确定并正在寻求专家建议。
你的问题更多的是关于为什么 C++11/C++14 是比以前的版本更好的 C++,以及为什么应该这样使用它的研究。
首先,正如评论所建议的那样,运算符 +=
和 -=
应该 return 是参考,而不是副本。如果它是成员函数运算符,它将 return *this
,但在您的情况下不是。只需将 return 类型更改为 Student &
.
崩溃的原因(也包含在注释中)是当复制构造函数或赋值执行时,array_d 的所有权由两个 'Student' 对象承担。当第二个被销毁时,它会尝试delete [] array_d
,它已经被第一个删除了。
赋值运算符显示return什么都没有。它应该 return *this;
评论指出您需要创建一个新数组并将每个元素从源复制到目标,因此有两个单独的数组。
然而,这是说明为什么 STL 如此有价值的关键时刻。如果您改用 std::vector<double>
,向量的赋值运算符会为您复制所有元素。
在两个运算符函数(+=
和 -=
)中,“else if
”子句没有任何价值。逻辑上,如果stu1.getClassL()
不是>= stu2.getClassL()
,那只能是<
,所以节省时间,去掉else if子句,以及包含的大括号。
给你的建议是使用 std::vector
并且让你自己不必实现赋值运算符、复制构造函数和析构函数。
不过话说回来,代码中有一些错误,更不用说复制构造函数了。
首先,复制构造函数应该分配一个全新的数组,并将传入值中的值从数组复制到新数组。这是 Student
class 的简化版本,只有两个成员——一个 double *
和一个表示数组中项目数的整数。
class Student
{
int num;
double *array_d;
public:
Student(const Student &student);
Student& operator=(const Student &student);
~Student() { delete [] array_d; }
Student() : array_d(0), num(0) {}
};
复制构造函数看起来像这样:
Student::Student(const Student &student) :
array_d(new double[student.num]), num(student.num)
{
for (int i = 0; i < num; ++i )
array_d[i] = student.array_d[i];
}
一旦有了这个,赋值运算符就很简单了 copy / swap:
Student& Student::operator=(const Student &student)
{
Student temp = student;
std::swap(d_array, temp.d_array);
std::swap(num, temp.num);
return *this;
}
基本上上面所做的就是制作传入对象的临时副本,将临时对象的内部结构与当前对象交换,然后临时对象与旧内部结构一起消失。除非 Student
的复制构造函数和析构函数正常工作(现在应该是这样),否则所有这一切都是不可能的。
接下来要考虑的是您对运算符 +=
和 -=
的整个想法。大多数程序员期望使用它的方式是在这种情况下:
Student a;
Student b;
// assume a and b are initialized and have data...
a += b;
如果您以不同的形式使用 +=
或 -=
,它会变得不直观而且很奇怪。因此,这些函数应该采用单个参数,而不是两个参数,并且 return 当前对象(它是您正在更改的当前对象)。
因此这些函数不应该是友元函数,而是 class Student
:
class Student
{
int num;
double *array_d;
public:
Student(const Student &student);
Student& operator=(const Student &student);
~Student() { delete [] array_d; }
Student() : array_d(0), num(0) {}
Student& operator += (const Student& rhs);
Student& operator -= (const Student& rhs);
};
然后 +=
的实现看起来像这样:
#include <algorithm>
//...
Student& operator+= (const Student& stu1)
{
int num_to_add = std::min(num, stu1.num);
for (int count = 0; count < num_to_add; ++count)
array_d[count] += stu1.array_d[count];
return *this;
}
同样,-=
看起来像上面那样。请注意使用 std::min
来确定要添加的数量,而不是使用 if / else
.