C++ 如何正确地重载&x2B;操作人员

C++ 如何正确地重载&x2B;操作人员,c++,operators,operator-overloading,C++,Operators,Operator Overloading,我试图定义一个简单的类来处理2d矩阵,称为Matrix2D01,但在使用+运算符时遇到了问题。 我有+=运算符,它工作正常 Matrix2D01& Matrix2D01::operator+=(Matrix2D01& mat2) { int i,j; for (i=0;i<M;i++) for (j=0;j<N;j++) mat[i][j]+=mat2[i][j]; return *this; } 当我尝试使用+运算符时,例如 mat

我试图定义一个简单的类来处理2d矩阵,称为Matrix2D01,但在使用+运算符时遇到了问题。 我有+=运算符,它工作正常

Matrix2D01& Matrix2D01::operator+=(Matrix2D01& mat2) {
    int i,j;
    for (i=0;i<M;i++)
        for (j=0;j<N;j++) mat[i][j]+=mat2[i][j];
return *this;
}
当我尝试使用+运算符时,例如

mat3=mat1+mat2;
编译器给出了一个错误:

../MatrixGames.cpp:17:12: error: no match for ‘operator=’ in ‘mat3 = Matrix2D01::operator+(Matrix2D01&)((* & mat2))’
如果有人能告诉我我做错了什么,我将不胜感激

谢谢

我还定义了一个=运算符

Matrix2D01& Matrix2D01::operator=(Matrix2D01& mat2) {
    if (this==&mat2) return *this;
    int i,j;
    for (i=0;i<M;i++) {
        delete [] mat[i];
    }
    delete [] mat;
    M=mat2.Dimensions()[0];
    N=mat2.Dimensions()[1];
    mat = new double* [M];
    for (i=0;i<M;i++) {
        mat[i]=new double [N];
    }
    for (i=0;i<M;i++)
        for (j=0;j<M;j++)
            mat[i][j]=mat2[i][j];
    return *this;
}  
Matrix2D01&Matrix2D01::operator=(Matrix2D01&mat2){
如果(this==&mat2)返回*this;
int i,j;

对于(i=0;i,我已经发布了我上个月为离散数学作业编写的矩阵类的代码

注意赋值运算符和复制构造函数是如何实现的您需要做的就是编写赋值运算符(我建议您也编写一个复制构造函数)

在您的代码中,您试图使用赋值运算符分配
结果
*值,但未定义赋值运算符,编译器也未找到匹配项
编写赋值运算符重载将解决此问题

如果您有一个复制构造函数,您可以执行以下操作:

Matrix2D01 Matrix2D01::operator+(Matrix2D01& mat2) {
    Matrix2D01 result = *this;
    result+=mat2;
    return result;
}
写入赋值运算符:

如果您对详细信息感兴趣,请点击这里:

您的操作数应该是
const
引用 无法使用临时文件对其进行初始化。(在本例中
运算符+
的返回值是临时值,因此不能 绑定到
运算符=
的非常量引用)

如果您的
操作符+
是成员,那么它也应该是常量。 毕竟,它不会修改调用它的对象。因此:

Matrix2D01& Matrix2D01::operator=( Matrix2D01 const& mat2 );
Matrix2D01& Matrix2D01::operator+=( Matrix2D01 const& mat2 );
Matrix2D01 Matrix2D01::operator+( Matrix2D01 const& mat2 ) const;
此外,您的赋值运算符已损坏。(想想会发生什么情况。) 如果在您释放资源后其中一个分配失败,则会发生这种情况 原始数据。)一般来说,如果您必须自行测试 赋值,赋值运算符已断开。

Herb Sutter主张(请阅读GotW,它写得很好,很有趣):

  • 不要将
    operator+
    作为成员函数,而是将其作为自由函数
  • 通过值传递一个对象,通过常量引用传递另一个对象。这使得在添加到临时对象时可以使用移动运算符
  • 按照
    运算符+=
    执行:

    Matrix2D01 operator+(Matrix2D01 a, const Matrix2D01 &b)
    {
        a += b;
        return a;
    }
    

  • 这个问题涉及到这个代码中的几个问题。最重要的是:<代码> > <代码>不是C++源代码中的一个有效字符。然后,<代码>运算符+<代码>应该是代码> const ,也可以采用非引用或const引用。同样,运算符+= 应该引用非引用或const引用。是否可以发布一个完整的测试用例?
    与“operator=”不匹配
    表明您没有为另一个
    Matrix2D01
    定义一个
    操作符(可能存在另一种类型,删除自动
    =
    操作),因此
    mat1+mat2
    起作用,但结果无法分配给
    mat3
    。请尝试创建
    Matrix2D01&Matrix2D01::operator=(const Matrix2D01&other)
    。感谢您的评论。很抱歉,这是一个输入错误。我确实定义了一个=运算符,我将把它添加到post
    运算符+
    修改并返回一个临时值。实现这一点最简单的方法是让它按值而不是按引用获取其参数(如果编译器能够做到,则无需在函数中显式创建临时函数,而且成本更低)。修改该值并返回。@user3004480您没有使用
    Matrix2D01
    classis定义赋值运算符,不是吗=运算符?是的defined@user3004480赋值运算符需要一个
    const
    参数。检查我添加到应答的链接将const添加到赋值运算符参数解决了问题.谢谢.非常成功!
        result=*this;
    
    Matrix2D01 Matrix2D01::operator+(Matrix2D01& mat2) {
        Matrix2D01 result = *this;
        result+=mat2;
        return result;
    }
    
    Matrix2D01& Matrix2D01::operator=( Matrix2D01 const& mat2 );
    Matrix2D01& Matrix2D01::operator+=( Matrix2D01 const& mat2 );
    Matrix2D01 Matrix2D01::operator+( Matrix2D01 const& mat2 ) const;
    
    Matrix2D01 operator+(Matrix2D01 a, const Matrix2D01 &b)
    {
        a += b;
        return a;
    }