In general, I implement the matrix class, I overload the addition and assignment operators in order to add two matrices and assign thirds as a result of the following action: C = A + B, but the assignment operator does not work. Only in a separate expression. Program Code:

#include <iostream> #include <locale.h> #include <cstdlib> #include <ctime> using namespace std; class Matrix { public: const static size_t n = 4; double arr[n][n]; Matrix() { for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) { arr[i][j] = 0; } } Matrix(int k) { for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) { arr[i][j] = rand()%101 ; } } void showArr(char); Matrix operator + (Matrix &a); Matrix operator = (Matrix &a); float operator[] (size_t i) { return arr[i][i]; } }; int main() { srand(time(NULL)); setlocale(0, ""); char A[2] = { "A" }; char B[2] = { "B" }; char C[2] = { "C" }; Matrix a(1); Matrix b(2); Matrix c; a.showArr(A[0]); b.showArr(B[0]); c=a+b; c.showArr(C[0]); system("pause"); } Matrix Matrix::operator+ (Matrix& a) { double c[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) a.arr[i][j]+= arr[i][j]; } return *this; } Matrix Matrix :: operator = (Matrix &a) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) arr[i][j] = a.arr[i][j]; } return *this; } void Matrix::showArr(char A) { cout << "Матрица " << A << ":" << endl; for (int i = 0; i < n; i++) { if (i == 0) { for (int j = 0; j < n; j++) if (arr[i][j] >= 10) { cout << arr[i][j] << " "; } else cout <<" "<< arr[i][j] << " "; } else { cout << endl; for (int j = 0; j < n; j++) if (arr[i][j] >= 10) { cout << arr[i][j] << " "; } else cout << " " << arr[i][j] << " "; } } cout << endl<< endl; } 
  • Matrix(int k) - just like the creators of the increment overload - but let's give us some garbage with an argument, just to distinguish, but we won't use it anyway. - Qwertiy
  • char A[2] = { "A" }; - there is something wrong with the types. - Qwertiy

2 answers 2

+ works like += . You do not use it there at all.

 Matrix Matrix::operator + (Matrix& a) { Matrix c; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) с.arr[i][j] = arr[i][j] + a.arr[i][j]; return с; } 
  • Yes, I tried to change something there, in the code, then I forgot to change it back. If it is not difficult, tell me how с return, so as not to change the fields of the original matrices. - Alexander Efremov
  • @AlexanderEfremov - Qwertiy

The addition operator is logically incorrect.

 Matrix Matrix::operator+ (Matrix& a) { double c[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) a.arr[i][j]+= arr[i][j]; } return *this; } 

It does not add two matrices, but assigns the values ​​of one matrix to another. Moreover, it assigns values ​​to the right operand from the addition sign, which only confuses the users of your program.

 a.arr[i][j]+= arr[i][j]; 

And besides, it returns a temporary object that corresponds to the left unchanged operand.

That is, if there is a record

 c = a + b; 

then the matrix b in it is not added to the matrix a and, moreover, the matrix c does not contain the result of addition.

That is, the code is completely incorrect.

As for the error, the problem is that this operator returns a temporary object, and the assignment operator is defined in such a way that it has a non-constant reference as a parameter.

 Matrix Matrix :: operator = (Matrix &a) ^^^^^^^^^ 

A non-constant link cannot be associated with a temporary object. Therefore, even if you do not take into account the shortcomings of the addition operator, to which I pointed out, this proposal

 c = a + b; 

should not compile.

Since in your class all members of a class have a public access class, the addition operator can be declared as a separate function of a non-class member.

It may look like this.

 const Matrix Matrix::operator +( const Matrix &a, const Matrix &b ) { Matrix result; for ( int i = 0; i < n; i++ ) { for ( int j = 0; j < n; j++ ) result.arr[i][j] = a.arr[i][j] + b.arr[i][j]; } return result; } 

An assignment operator, which is naturally a class method as opposed to an addition operator, must have a constant reference as a parameter

 Matrix Matrix::operator =( сщтые Matrix &a ) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) arr[i][j] = a.arr[i][j]; } return *this; } 

Your definition of the index operator is also not clear. In addition to the fact that it should at least have a return type of double , not float

 float operator[] (size_t i) ^^^^^ { return arr[i][i]; } 

he doesn't actually make sense.

You have other shortcomings in the program.

For example, it is unclear what value the parameter k in this constructor has

 Matrix(int k) { for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) { arr[i][j] = rand()%101 ; } } 

Since it is not used anywhere.

Moreover, in both constructors, you must use the static constant n , not the magic number 4 .

If you need to have two constructors, one of which initializes the array with zeros and the other with random values, then you could bvtnm jlby rjycnhernjh b declare the tuj parameter as bool and specify the default argument.

for example

 Matrix( bool random = false ); 

The showArr method showArr correct to declare with a parameter of type const char * and pass not one character as an argument, but a string.

In addition, the method should be made constant:

 void showArr( const char * ) const; 

And with the implementation of this method you have something strange and incomprehensible. :) I am sure that it is also incorrect.

  • Um .. And since when *this is a temporary object? And the operator's argument in the form of a variable when declaring a transfer by reference is also not a temporary object. As for showArr , yes, there is tin there, but it seems to work. - Qwertiy
  • @Qwertiy The return value of the function is a temporary object that is created by copying the * this object. - Vlad from Moscow
  • Oh, right. I'm used to something that when they do return *this; , then return the link, and looked up that the statement is not declared as Matrix & operator + (Matrix &a); , but simply Matrix operator + (Matrix &a); . - Qwertiy
  • The showArr method showArr implemented in such a way that the matrix looks even in units. Why make the method constant? - Alexander Efremov
  • @AleksandrEfremov This method does not change the object itself, and therefore, so that it can be called for constant objects, for example, when the matrix is ​​passed by a constant link to a function, or when a constant matrix is ​​returned from a function, then this method could be applied . - Vlad from Moscow