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"); }
for (int i = 0; i<k; i++) for (int j = 0; j<k; j++) if (i == j)- brrrrrrrrrrrrrr - Qwertiy ♦