C++:使用重载的复合赋值运算符时出现运行时错误

标签 c++ overloading dynamic-memory-allocation

我有一个程序使用一个类来动态分配一个数组。我有重载运算符,可以对来自该类的对象执行操作。

当我测试这个程序时,重载的 += 工作,但 -= 不工作。 当试图运行重载的 -= 时程序崩溃,我得到以下运行时错误: p>

malloc: * error for object 0x7fd388500000: pointer being freed was not >allocated * set a breakpoint in malloc_error_break to debug

在私有(private)成员变量中,我这样声明数组:

double* array_d;

然后我在重载的构造函数中动态分配数组:

Students::Students(int classLists)
{
    classL = classLists;
    array_d = new double[classL];
}

我将以下两个重载构造函数定义为 Students 类的友元:

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;
}

奇怪的是 += 有效但 -= 无效,因为它们实际上是相同的。我怀疑我的问题出在我的动态内存分配上,但我不确定并正在寻求专家建议。

最佳答案

给你的建议是使用 std::vector 并且让你自己不必实现赋值运算符、复制构造函数和析构函数。

但话说回来,代码中有一些错误,更不用说复制构造函数了。

首先,复制构造函数应该分配一个全新的数组,并将传入值中的值从数组复制到新数组。这是您的 Student 类的简化版本,只有两个成员——一个 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;

如果您以不同的形式使用 +=-=,它会变得不直观而且很奇怪。因此,这些函数应该采用单个参数,而不是两个参数,并返回当前对象(它是您正在更改的当前对象)。

因此这些函数不应该是友元函数,而是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 的原始代码。

关于C++:使用重载的复合赋值运算符时出现运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32706584/

相关文章:

c - 为函数 C 中复制的指针赋值

c++ - 如何制作一个容器来容纳任何类型的指针

typescript - 如何从 typescript 中的参数推断或推导返回类型

c++ - 使用下标重载的单链表排序

java - 为什么按值传递在此示例中有效?

c++ - 使用 operator new/malloc 分配的内存块能否在程序执行结束后持续存在?

c++ - 段错误,在动态分配的结构中动态分配结构

c++ - 优化查找复数作为输入

c++ - x265编码器,将yuv转为x265_picture

c++ - 生成具有负边权重且没有负循环的随机图