1

这是我的代码的一部分,当我编译它时,它说 1: no match for operator = 2: no known conversion for argument 1 from 'Matrix' 到 'Matrix&' 但是如果我删除 operator + 部分它在哪里工作问题?!:|

gcc 错误:“'z = Matrix::operator+(Matrix&)((* & y))' 中的 'operator=' 不匹配' 候选是:atrix& Matrix::operator=(Matrix&) 参数 1 的未知转换来自 '矩阵'到'矩阵&'"

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;
}   

};

4

1 回答 1

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

他们都有同样的问题——参数类型应该是const Matrix&(就像在复制构造函数中一样)。否则,您无法将临时对象传递给操作员。

于 2013-03-17T10:05:50.730 回答