Reputation: 1541
here is a part of my code, when i compile it, it says 1: no match for operator = 2: no known conversion for argument 1 from 'Matrix' to 'Matrix&' but if i remove the operator + part it works where is the problem?! :|
gcc errors: "no match for 'operator=' in 'z = Matrix::operator+(Matrix&)((* & y))' candidate is: atrix& Matrix::operator=(Matrix&) no known conversion for argument 1 from 'Matrix' to 'Matrix&' "
class Matrix {
//friend list:
friend istream& operator>>(istream& in, Matrix& m);
friend ostream& operator<<(ostream& in, Matrix& m);
int** a; //2D array pointer
int R, C; //num of rows and columns
static int s1, s2, s3, s4, s5;
public:
Matrix();
Matrix(const Matrix&);
~Matrix();
static void log();
Matrix operator+ (Matrix &M){
if( R == M.R && C == M.C ){
s4++;
Matrix temp;
temp.R = R;
temp.C = C; temp.a = new int*[R];
for(int i=0; i<R; i++)
temp.a[i] = new int[C];
for(int i=0; i<R; i++)
for(int j=0; j<C; j++)
temp.a[i][j] = a[i][j] + M.a[i][j];
return temp;
}
}
Matrix& operator = (Matrix& M){
s5++;
if(a != NULL)
{
for(int i=0; i<R; i++)
delete [] a[i];
delete a;
a = NULL;
R = 0;
C = 0;
}
R = M.R;
C = M.C;
a = new int*[R];
for(int i=0; i<R; i++)
a[i] = new int[C];
for(int i=0; i<R; i++)
for(int j=0; j<C; j++)
a[i][j] = M.a[i][j];
return *this;
}
};
Upvotes: 1
Views: 597
Reputation: 92241
Matrix operator+ (Matrix &M){
Matrix& operator= (Matrix &M){
They both share he same problem - the parameter type should be const Matrix&
(just like in the copy constructor). Otherwise you cannot pass temporary objects to the operators.
Upvotes: 2