错误:与“ operator +”不匹配(操作数类型为“ int”和“ class”)

时间:2019-05-04 23:41:02

标签: c++

我的代码有一些问题,我不知道这是怎么回事。 错误是:错误:

与“ operator +”不匹配(操作数类型为“ int”和“ Matrix2D”)   m = 2 + m; //运算符+(3)

我的代码是:

Matrix2D Matrix2D :: operator + (const int & number){
TipoBase **tmp;

tmp = new int * [rows];

    for(int i = 0; i < rows; i++){
        tmp[i] = new int [cols];
    }

    for(int i = 0; i < rows; i++){
        for(int j = 0; j < cols; j++){
            tmp[i][j] = info[i][j] + number;
        }
    }

return(tmp);

}

1 个答案:

答案 0 :(得分:1)

您已经将operator+作为成员函数的重载,它允许您执行m + 2

如果您想做2 + m,则还必须提供重载作为(朋友?)功能

Matrix2D operator+(const int& i, const Matrix2D& m){
  return m + i;
}
相关问题