C ++: runtime error when using overloaded assignment operators

I have a program that uses a class to dynamically allocate an array. I overloaded operators that perform operations on objects of this class.

When I test this program, overloaded + = works, but - = does not work. When the program crashes when I try to start the overloaded - =, I get the following runtime error:

malloc: * error for object 0x7fd388500000: no pointer was freed> highlighted * set breakpoint in malloc_error_break for debugging

In private member variables, I declare an array as follows:

double* array_d;

Then I dynamically allocate the array in the overloaded constructor:

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

, , , L. 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;
}

, + = , - = , . , , .

+4
2

, , - std::vector , .

, , , .

-, . Student - a 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.

+3

, ++ 11/++ 14 ++, , .

-, , += -= , . --, return *this, . Student &.

, , , array_d "". , delete [] array_d, .

. * this;

, , .

, , STL . std::vector<double> , .

( +=, -=) "else if" . , stu1.getClassL() >= stu2.getClassL(), <, else if, .

+1

Source: https://habr.com/ru/post/1608395/


All Articles