编译错误:没有匹配的重载操作符
Compilation error : No match for overloaded operator
这是我的一部分代码,当我编译它时,它说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&
(就像复制构造函数一样)。否则,不能将临时对象传递给操作符。
相关文章:
- 重载操作符+:表达式必须是整型或无作用域枚举类型
- 重载操作符
- 如何重载操作符==外模板类使用友元函数
- 重载*操作符,使其在左右两边都工作
- 重载操作符<对于非随机迭代器
- 在c++中重载操作符的时间和原因
- 如何在c++中重载=操作符来通过引用进行复制
- 如何在c++中获取定义为友元的重载操作符的地址
- 使用重载操作符的文件操作表达式没有给出预期的结果
- 重载操作符()
- 重载操作符()并在类内使用
- 类中的重载操作符+
- 定时使用重载操作符
- c++带类的重载操作符
- 用列表容器重载[]操作符
- 重载操作符=
- 任何重载操作符()的静态检测
- 重载操作符以处理类对象
- 在使用另一个类的类中重载操作符==
- 派生类和基类中的重载操作符不同