2

This is a class with an object 'matrix' that stores a 2D dynamic array. I need to be able to add 2 matrices and put the sum of the elements into the result matrix. (i.e: c[1][1] will equal a[1][1] + b[1][1]). But I want to implement it the following way:

Square_Matrix a,b,c;
c = a + b;

Here are my two overloaded operators, the '=' one works fine outside of '+' (So a = b = c works just fine, matrices get copied over). Unfortunately, I don't get an error on my IDE, the program just closes and says "Square_Matrix has stopped working". How can I fix this?

I'm also not too sure that I implemented my '+' correctly, someone said "return *this" won't do anything.

//.h file
Square_Matrix& operator=(const Square_Matrix& Par2);
Square_Matrix& operator+(const Square_Matrix& Par3);

//.cpp file    
Square_Matrix& Square_Matrix::operator=(const Square_Matrix& Par2){
    if (size != Par2.size){
        cout << "Matrices are of different size" << endl;
    } else {
        for (int i = 0; i < size; i++){
            for (int j = 0; j < size; j++){
                 matrix[i][j] = Par2.matrix[i][j];
            }
        }
    }
}

Square_Matrix& Square_Matrix::operator +(const Square_Matrix& Par3){
    Square_Matrix result;
    result.Set_Size(Par3.size);
    for (int i = 0; i < Par3.size; i++){
        for (int j = 0; j < Par3.size; j++){
            result.matrix[i][j] = Par3.matrix[i][j]+matrix[i][j];
        }
    }
return *this;
}
Seb
  • 1,966
  • 2
  • 17
  • 32
  • 2
    Your `operator=` should end with `return *this;`. Your `operator+` should end with `return result;` and should not be returning a reference. – Brian Bi Feb 22 '14 at 19:25
  • @remyabel: From `operator+`? That's not the reason to not return a reference. You don't return a reference because `operator+` should be creating a new object local to the function. And if you returned a reference to that, it would be a dangling reference. – Benjamin Lindley Feb 22 '14 at 19:29
  • See also [here](http://stackoverflow.com/a/4421719/509868) (look for the correct definition of `operator+` and `operator+=`) – anatolyg Feb 22 '14 at 19:30
  • Sorry that was a typo, my = function does have return *this. And return result seemed to clear up the errors thanks – Seb Feb 22 '14 at 19:33
  • @Foxic -First, having operator = check for sizes is an abuse of what operator = is supposed to do. The operator= should serve one purpose and one purpose only, and that is to make copies. It isn't there for you to code "business rules" such as not making the copy due to a size mismatch. If you want a function that checks for sizes, then create another member function for this purpose -- do not use operator = for this. – PaulMcKenzie Feb 22 '14 at 19:44
  • @Foxic - Just to conclude, having operator= make fake copies as yours does opens up your program for a whole host of hard-to-find bugs. The way your operator= should work is to replace the Matrix on the left of the = with an exact copy of the matrix on the right of the =. Nothing more, nothing less. Again, if you want a function that conditionally copies based on size, create an Assign() function that does this type of checking and copying. – PaulMcKenzie Feb 22 '14 at 19:46
  • The + operator should not return a reference. It has to return a value. So, your `return *this;` is wrong. In addition, `this` does not contain the result, does it? How can returning it give the correct value to the equation? – ThunderGr Feb 22 '14 at 19:48

1 Answers1

2

Your Square_Matrix& Square_Matrix::operator=(const Square_Matrix& Par2) should end with

return *this;

and your Square_Matrix& Square_Matrix::operator +(const Square_Matrix& Par3) should end with

return result;

And change

Square_Matrix& Square_Matrix::operator +(const Square_Matrix& Par3)

to

Square_Matrix Square_Matrix::operator +(const Square_Matrix& Par3)
Avt
  • 16,927
  • 4
  • 52
  • 72
  • 1
    The operator + should not return "result" if the return type is a reference. The "result" is a local variable, and it is UB to return a reference to a local variable. – PaulMcKenzie Feb 22 '14 at 19:40
  • @PaulMcKenzie, you are right. Sorry I forget to add the last requirement. – Avt Feb 22 '14 at 19:45