Please help me understand this error. He is still just learning, so he spent a lot of time trying to figure it out.

Mistake

class Matrix { private: double** M; public: int n; int m; class wrong_size{}; class wrong_count{}; Matrix(int height, int width); ~Matrix(); Matrix operator+(Matrix& A); Matrix operator-(Matrix& A); Matrix operator*(Matrix& A); double* operator[](int i); friend istream& operator >>(istream& input, Matrix& A); friend ostream& operator <<(ostream& output, Matrix& A); }; Matrix::Matrix(int height = 3, int width = 4) : n(height), m(width) { M = new double*[n]; for (int i = 0; i < n; i++) M[i] = new double[m]; } Matrix::~Matrix() { for (int i = 0; i < n; i++) delete[] M[i]; delete[] M; cout << "Delete"; } Matrix Matrix::operator+(Matrix& A) { if(n == An && m == Am) { Matrix X(n, m); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) X[i][j] = M[i][j] + A[i][j]; return X; } else throw wrong_size(); } Matrix Matrix::operator-(Matrix& A) { if (n == An && m == Am) { Matrix X(n, m); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) X[i][j] = M[i][j] - A[i][j]; return X; } else throw wrong_size(); } Matrix Matrix::operator*(Matrix& A) { if(An == m) { Matrix X(n, m); for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) { X[i][j] = 0; for (int k = 0; k < n; k++) X[i][j] = M[i][k] * A[k][j]; } return X; } else throw wrong_count(); } double* Matrix::operator[](int i) { return M[i]; } istream& operator>>(istream& input, Matrix& A) { for (int i = 0; i < An; i++) for (int j = 0; j < Am; j++) input >> AM[i][j]; return input; } ostream& operator<<(ostream& output, Matrix& A) { for (int i = 0; i < An; i++) { for (int j = 0; j < Am; j++) output << A[i][j] << " "; output << endl; } return output; } void function(Matrix& A) { for (int i = 0; i < An; i++) for (int j = 0; j < Am; j++) if (fmod(A[i][j], 2) > 0) { A[i][j] *= 2; cout << A[i][j] << " "; } } void main() { setlocale(LC_ALL, "Russian"); try { int n1, m1; cout << "Введите n1 & m1: "; cin >> n1 >> m1; Matrix M1(n1, m1); cin >> M1; int n2, m2; cout << "Введите n2 & m2: "; cin >> n2 >> m2; Matrix M2(n2, m2); cin >> M2; Matrix M3; int choice; cout << "Выберите действие:\n1.Сумма\n2.Разность\n3.Произведение\n4.Увеличение нечетных элементов\n"; cin >> choice; switch (choice) { case 1: M3 = M1 + M2; cout << "Сумма: " << endl << M3; break; case 2: M3 = M1 - M2; cout << "Разность: " << endl << M3; break; case 3: M3 = M1 * M2; cout << "Произведение: " << endl << M3; break; case 4: Matrix M4; cin >> M4; function(M4); break; } } catch(Matrix::wrong_size) { cout << "Вы ввели неправильный размер матриц." << endl; } catch(Matrix::wrong_count) { cout << "Ошибка, во время вычисления" << endl; } system("pause"); } 

1 answer 1

No assignment operator or copy constructor; as a result, the compiler generates them and they perform surface copying - not the matrix, but the pointer to it.

After that, everything in the destructor also falls due to double deletion.

This is so, offhand. I can not guarantee that this is where the errors end - I looked superficially, like a generated constructor :)

Update

And what exactly should be in the copying designer?

The main thing is to create a matrix , i.e. memory allocation, followed by copying the elements of the copied matrix into it.

In an assignment statement — if the available memory allocated to a matrix is ​​enough — overwriting it with the corresponding elements, if not — releasing it, and selecting a new one, then copying the elements of the copied matrix into it.

Update2

Well, what is there to understand?

 Matrix::Matrix(const Matrix&Mt) : n(Mt.n), m(Mt.m) { M = new double*[n]; for (int i = 0; i < n; i++) M[i] = new double[m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) M[i][j] = Mt.M[i][j]; } 

Like that. Assignment - similarly, only taking into account that memory is already allocated.

  • And what exactly should be in the copying designer? - Eugene Rybalko
  • @ Zhenya Rybalko copying the matrix - Croessmah
  • To be honest, I still do not understand how to implement all this. As I have said before, I am still learning, having read on the Internet, I still haven't really figured it out. It would not be bad if the edited code was shown, but thanks anyway - Eugene Rybalko
  • See amended answer. - Harry
  • All, thank you so much, figured out - Eugene Rybalko