0

I've recently started learning C++, right now I'm working on a Matrix Class. I'm trying to overload operators and it turned out to be more difficult than I thought. So I've overloaded '=' and '+', first one works perfectly fine when I just want to set one matrix equal to another, but when I do something like 'matrix = matrix1 + matrix2' it crashes with no error. I'd be very thankful if someone helps me. Here's my code:

class Matrix
{
private:
    int lines , columns;
    int *Matrix_Numbers;
public:
    Matrix();

    Matrix(int n , int m)
    {
        lines = n , columns = m;
        Matrix_Numbers = new int[lines * columns];
    }

    Matrix & operator = (Matrix &mat);

    Matrix & operator + (Matrix &mat);

    ~Matrix()
    {
        delete Matrix_Numbers;
    }
};

Matrix & Matrix::operator = (Matrix &mat)
{
    this -> lines = mat.lines;
    this -> columns = mat.columns;
    int i , j;
    for(i = 0 ; i < lines ; i++)
    {
        for(j = 0 ; j < columns ; j++)
        {
            this -> Matrix_Numbers[i * (this -> columns) + j] = mat(i , j);
        }
    }
    return *this;
}

Matrix & Matrix::operator + (Matrix &mat)
{
    Matrix result(lines , columns);
    if(mat.lines == lines && mat.columns == columns)
    {
        int i , j;
        for(i = 0 ; i < lines ; i++)
        {
            for(j = 0 ; j < columns ; j++)
            {
                result.Matrix_Numbers[i * columns + j] = Matrix_Numbers[i * 
                                            columns + j] + mat(i , j);
            }
        }
    }
    else
    {
        cout << "Error" << endl;
    }
    return result;
}

Of course it's just a part of my code, there's more, but I thought that this is the broken part. Let me know if you need more information:)

Geoffrey
  • 10,843
  • 3
  • 33
  • 46
zavullon
  • 43
  • 1
  • 6
  • 2
    1. Which line does the program crash on? 2. Create a [mcve]. – eerorika Feb 25 '18 at 01:16
  • 4
    You need to return by value from your `operator+`, right now you are returning a dangling reference. That will result in UB. – super Feb 25 '18 at 01:17
  • Do you have a destructor? You probably should be using smart pointers. – rieux Feb 25 '18 at 01:17
  • Related, but not a duplicate: https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three – Miles Budnek Feb 25 '18 at 01:19
  • `operator+()` needs to return by value, not by reference (in other words, remove the `&` from the return type). As is, it returns a reference to a local variable that ceases to exist when the function returns. That causes the caller to have undefined behaviour if it uses that reference. – Peter Feb 25 '18 at 02:13
  • Also, use the `const` in more places. An expression like `a = b + c` is not expected to change `b` or `c` so, if they are passed by reference, they should be qualified as `const`. – Peter Feb 25 '18 at 02:16

1 Answers1

0

Your operator+ method returns a reference to the result. The only problem is that result is a local variable which means it gets destroyed upon the method returning. You should return it by value instead and let the compiler optimize stuff.

Like others pointed out in the comments, you should use const whenever possible so your operator+ can actually be called with constant parameters.

eesiraed
  • 4,626
  • 4
  • 16
  • 34