编译错误:没有匹配的重载操作符

Compilation error : No match for overloaded operator

本文关键字:重载 操作符 错误 编译      更新时间:2023-10-16

这是我的一部分代码,当我编译它时,它说1:不匹配operator =2:未知参数1从'Matrix'到'Matrix&'的转换但是如果我去掉运算符+部分,它就可以工作了问题出在哪里?: |

gcc错误:"在'z = Matrix::operator+(Matrix&)((* &)y) '候选人:atrix&矩阵::操作符= (Matrix&)没有已知参数1从'Matrix'到'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;
}   

};

Matrix operator+ (Matrix &M){
Matrix& operator= (Matrix &M){

它们都有同样的问题——参数类型应该是const Matrix&(就像复制构造函数一样)。否则,不能将临时对象传递给操作符。