enter image description here Good day! It is necessary to overload the operator + for the addition of two two-dimensional arrays. Tried to overload like this, but it causes an exception.

friend Matrix operator +(const Matrix &right, const Matrix &left) { Matrix result (right.n,right.m); for (int i = 0; i < right.n; i++) { for (int j = 0; j < right.m; j++) { result.mas[i][j] += right.mas[i][j]+ left.mas[i][j]; } } return result; } 

How to fix?

Update

So far, poorly versed with designers, but here's what happened.

 Matrix() { n = n; m =m; mas = new int*[n]; for (int i = 0; i < n; i++) { mas[i] = new int[m]; } } 

This is the default constructor, I hope.

And here's another

 Matrix (int n, int m) : n(n), m(m) { mas = new int*[n]; for (int i = 0; i < n; i++) { mas[i] = new int[m]; } } 
  • what exceptions? - ampawd
  • @choko And the sizes of these arrays of operands of the operator are the same? - Vlad from Moscow
  • @VladfromMoscow match up - choko
  • 2
    @choko I have doubts about this. - Vlad from Moscow
  • one
    First, the problem most likely has nothing to do with the code you give. As already noted, a typical source of such problems is a violation of the Rule of Three: the copy constructor or the copy assignment operator is incorrectly written. Secondly, if you have already “fixed something”, then take the trouble to fix it and the code here too. - AnT

1 answer 1

In a good way, you need to initialize the array in the constructor. You allocated memory, now there — in the array — completely random values ​​... This is for the Matrix (int n, int m) constructor Matrix (int n, int m) . In the Matrix() constructor, everything is generally fun - what values ​​do you assign n and m ? Yes, the same random that they were .

Even if you correct the constructor, when adding, you do not replace the non-initialized values ​​of the array, but add the sum of the elements to them , so there will still be garbage ...

Well, be sure to check the equality of the sizes of summable arrays! If not by generating an exception, then at least assert ...

So, throw away the default constructor, make one like this:

 Matrix(int n = 1, int m = 1, int value = 0): n(n), m(m) { mas = new int*[n]; for (int i = 0; i < n; i++) { mas[i] = new int[m]; for(int j = 0; j < m; ++j) mas[i][j] = value; } } 

B will have it both for you and for the reaper (and the usual constructor, and by default - create an 1x1 array), and even on the duda igrets - fill the array with default values ​​...

Well, in the operator + write not

 result.mas[i][j] += right.mas[i][j]+ left.mas[i][j]; 

but

 result.mas[i][j] = right.mas[i][j]+ left.mas[i][j]; 

and add a check at least assert(left.n == right.n && left.m == right.m); or

 if (left.n != right.n || left.m != right.m) throw std::exception("Matrix sizes not matched in operator+()"); 

Well, and do not forget, as you have already written, the copy constructor. And the assignment operator does not hurt. About them already told here: Are the copy constructor and the assignment operator correct? How to improve?

  • Still, the value parameter is superfluous. Nobody needs a matrix filled with units :) - Pavel Mayorov
  • @PavelMayorov Well, yes, it’s better rubbish ... By the way, you didn’t write to the C ++ Standardization Committee to throw out the corresponding parameter from the vector constructor? :) - Harry
  • std::vector is just a variable-size array. There, this designer is very much in the subject. The author also explicitly writes a matrix in a mathematical sense - what the same addition operation says. Matrix filled with units does not make sense! - Pavel Mayorov
  • @PavelMayorov Lord, well, if you don’t write the third parameter, there will be zeros. Or does the matrix filled with zeros also have no meaning? :) - Harry
  • Of course it does. This is the zero matrix. It would only make sense to leave this behavior and leave, and the parameter that no one will transmit will be removed. - Pavel Mayorov