I created a class of matrices and overloaded the operation ++ for it (adding the identity matrix to the matrix), but for some reason the program does not work, it generates an error detected before normal block when it starts ... If you remove the ++ operation, everything works.

What is the mistake and how to fix it? UPD. Errors corrected, everything works.

#include<iostream> #include<iomanip> #include<fstream> using namespace std; class Matrix { int *matrix; int n; public: Matrix(int); Matrix() { matrix = NULL; } int &operator()(int i, int j) { return matrix[i *n + j]; } friend ostream &operator<< (ostream &s, const Matrix &c); // перегруженный оператор вывода friend istream &operator >> (istream &s, Matrix &c); // перегруженный оператор ввода ~Matrix() // десструктор класса Matrix { delete[] matrix; } friend Matrix& operator++(Matrix&);//оператор ++ friend Matrix& operator++(Matrix&, int); Matrix &operator=(Matrix &c); }; Matrix::Matrix(int an) // конструктор с параметрами { n = an; matrix = new int[n*n]; } Matrix &Matrix::operator=(Matrix &c) { for (int i = 0; i < cn; i++) { for (int j = 0; j < cn; j++) { this->matrix[i *( this->n) + j] = c.matrix[i * cn + j]; } } return *this; } istream& operator >> (istream &s, Matrix &c)//ВВОД { for (int i = 0; i<cn; i++) { for (int j = 0; j<cn; j++) { s >> c.matrix[i*cn + j]; } } return s; } ostream &operator<< (ostream &s, const Matrix &c)//ВЫВОД { for (int i = 0; i<cn; i++) { for (int j = 0; j<cn; j++) { s << setw(5) << c.matrix[i * cn + j]; } s<<'\n'; } return s; } Matrix& operator++( Matrix&c) //ОПЕРАЦИЯ ++M { for (int i = 0; i < cn; i++) c.matrix[i * cn + i]++; return c; } Matrix& operator++(Matrix&c, int) //ОПЕРАЦИЯ M++ { Matrix Y(cn); Y = c; for (int i = 0; i < cn; i++) c.matrix[i * cn + i]++; return Y; } void main() { setlocale(LC_ALL, ""); int n; cout <<"Введите размер матрицы:"<<'\n'; cin >> n; Matrix A(n); cout << "Введите элементы матрицы:" << '\n'; cin >> A; cout << "Матрица:" << '\n'; cout << A << '\n';; Matrix Z(n); Z = A; cout << "Копия матрицы:" << '\n'; cout << Z << '\n';; cout << "++Матрица:" << '\n'; cout << (Z = ++A) << '\n';; cout << "Матрица++:" << '\n'; cout << (Z = A++) << '\n';; system("pause"); } 
  • one
    What specifically does not work? - Timur Yalimov Nov.
  • for (int i = 0; i<k; i++) for (int j = 0; j<k; j++) if (i == j) - brrrrrrrrrrrrrr - Qwertiy
  • Gives an error heap corruption detected before normal block - Kirill

1 answer 1

You have a strange appeal to the elements of the matrix.

Instead:

 for (int i = 0; i<k; i++) { this->matrix[(i - 1)*k + i - 1]++; } 

Try:

 for (int i = 0; i < k; i++) { for (int j = 0; j < k; j++) { if ( i == j ) { this->matrix[ i * k + j ]++; } } } 

The same applies to all your calls to the elements of the matrix in all other operators.

  • It did not help ... Google says that the error heap corruption detected before the normal block means an exit for the array memory, incorrect memory management, etc. - Kirill
  • Have you changed the address to the matrix everywhere? You have it wrong in the operators << >> (). - Timur Yalimov Nov.
  • Changed. Corrected. The program works. If you are interested in what has changed, then the code in the question is fixed. - Kirill