Segmentation Fault on return

I am creating a Matrix class for a project I am working on. To do so, I am overloading all basic operators.

Here is an example of my overloaded + operator:
1
2
3
4
5
6
7
8
9
10
11
  Matrix Matrix::operator+(const Matrix& u) const {
    if(rows!=u.getRows() || cols!=u.getCols()) {
        std::cout << "matricies are not the same size" <<std::endl;
        return *this;
    }
    Matrix newMatrix(rows,cols);
    for(int idx=0;idx<rows;idx++) {
        newMatrix[idx]=(*this)[idx]+u[idx];
    }
    return newMatrix;
  }


my function correctly sums *this and u into newMatrix and I can output newMatrix on the line before the return. However, I get a segmentation fault in the return for this and all other overloaded operators. The only exception is operators such as += which return *this instead of newMatrix which work correctly.
Are you working with pointers? Maybe your operator= is incorrect
Yes, it seems your copy constructor or operator= absent or incorrect.
Topic archived. No new replies allowed.