Hi, I'm programming a class for manipulating real matrices. I'm having some problems when doing a sentence like "c = a+b". The code is:
class Matrix {
public:
Matrix(int n, int m);
Matrix(Matrix* copy);
~Matrix();
Matrix& operator=(Matrix& copy);
Matrix operator+(Matrix& plus);
float GetEntry(int i, int j);
float GetEntry(int i);
int GetHeight();
int GetWidth();
void SetEntry(int i, int j, float x);
void SetEntry(int i, float x);
Matrix Minor(int i, int j);
Matrix Reduce(int i, int j, float x, bool mode);
Matrix TriangularForm();
Matrix TriangularForm(Matrix* left, Matrix* right);
int Rank();
private:
float* Entry;
int Height;
int Width;
};
Matrix& Matrix::operator=(Matrix& copy) {
int n;
int m;
int i;
n = copy.GetHeight();
m = copy.GetWidth();
if(n != Height || m != Width) {
delete[] Entry;
Entry = new float[n * m];
Height = n;
Width = m;
}
for(i = 0; i < n * m; i++){
Entry[i] = copy.GetEntry(i);
}
return *this;
}
Matrix Matrix::operator+(Matrix& plus) {
int i;
Matrix ans = *this;
for(i = 0; i < Height * Width; i++) {
ans.Entry[i] += plus.GetEntry(i);
}
return ans;
}
I'm getting the following error when compiling:
src/main.cpp: In function ‘int main()’:
src/main.cpp:34: error: no match for ‘operator=’ in ‘c = Matrix::operator+(Matrix&)(((Matrix&)(& b)))’
src/matrix.h:23: note: candidates are: Matrix& Matrix::operator=(Matrix&)
I can't see why it doesn't work. Could you help me? Thank you in advance