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:)