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

C++ c++;如何正确地重载&x2B;操作人员,c++,C++,我需要为我的研究实现一个矩阵类,现在我被操作符+重载所困扰。 这是我的矩阵.cpp代码: template<class type> matrix<type> matrix<type>::operator+ (const matrix<type>& matObj){ matrix<type> newMat(this->WIDTH, this->HEIGHT, 0); for (int i = 0; i <

我需要为我的研究实现一个矩阵类,现在我被操作符+重载所困扰。 这是我的
矩阵.cpp
代码:

template<class type>
matrix<type> matrix<type>::operator+ (const matrix<type>& matObj){
matrix<type> newMat(this->WIDTH, this->HEIGHT, 0);
    for (int i = 0; i < this->HEIGHT; i++)
        for (int j = 0; j < this->WIDTH; j++)
            newMat.array[WIDTH * i + j] = matObj.array[WIDTH * i + j] + this->array[WIDTH * i + j];

    return newMat;
}
int main() {

    vector< vector<int>> v = {
        {1, 2, 3},
        {4, 5, 6},
        {1, 2, 9}
    };
    math::matrix<int> mat1(v), mat2;
    mat2 = mat1+mat1;

    for (int i = 0; i < mat2.cols();  i++) {
        for (int j = 0; j < mat2.rows(); j++) {
            cout << mat2[i][j] << ", ";
        }
        cout << endl;
    }

    cin.get();
    return 0;
}
假设所有构造函数和其他代码都编写正确。 所以问题是,当我尝试运行程序时,它会在线失败:

mat2 = mat1+mat1;

当我试图在返回语句之前在
matrix.cpp
中打印
newMat
时,它会正确地打印它。当然,我已经实现了=运算符,它工作得很好。有什么建议吗?

实现
+
的简单方法如下

struct example {
  int state = 0;

  example& operator+=( example const& rhs )& {
    // increment state here; change in real code
    state += rhs.state;
    return *this;
  }
  friend example operator+( example lhs, example const& rhs ) {
    lhs += rhs;
    return std::move(lhs);
  }
  example( example&& ) = default; // ensure this is efficient
  example& operator=( example&& ) = default; // ensure this is efficient
  example& operator=( example const& ) = default; // this can be expensive
  example( example const& ) = default; // this can be expensive
};
更具体地说

template<class T>
struct matrix {
  std::vector< std::vector<T> > state;

  matrix & operator+=( matrix const& rhs )& {
    // increment state here; change in real code
    for (std::size_t i = 0; i < (std::min)(state.size(), rhs.state.size(); ++i) {
      for (std::size_t j = 0; j < (std::min)(state[i].size(), rhs.state[i].size(); ++j) {
         state[i][j] += rhs.state[i][j];
      }
    }
    return *this;
  }
  friend matrix operator+( matrix lhs, matrix const& rhs ) {
    lhs += rhs;
    return std::move(lhs);
  }
  matrix( matrix&& ) = default; // ensure this is efficient
  matrix& operator=( matrix&& ) = default; // ensure this is efficient
  matrix& operator=( matrix const& ) = default; // this can be expensive
  matrix( matrix const& ) = default; // this can be expensive
}
模板
结构矩阵{
std::vector状态;
矩阵和运算符+=(矩阵常数和rhs)和{
//此处为增量状态;实际代码中的更改
对于(std::size_t i=0;i<(std::min)(state.size(),rhs.state.size();++i){
对于(std::size_t j=0;j<(std::min)(状态[i].size(),rhs.state[i].size();++j){
状态[i][j]+=右侧状态[i][j];
}
}
归还*这个;
}
友元矩阵运算符+(矩阵lhs、矩阵常数和rhs){
lhs+=rhs;
返回标准::移动(lhs);
}
矩阵(矩阵&&)=默认值;//确保这是有效的
矩阵和运算符=(矩阵&&)=默认值;//确保这是有效的
矩阵和运算符=(矩阵常数&)=默认值;//这可能会很昂贵
矩阵(矩阵常数&)=默认值;//这可能很昂贵
}
=default
适用于
matrix(matrix&&)
等,因为我遵循了0的规则——我将数据存储留给了一个专门的类(
std::vector
),并且它具有高效的移动语义,所以我自动获取它


如果你用
state
交换一个具有width*height元素的
std::vector
,你只需要调整访问它的方式。(这更有效,但我试图保持它的简单)。

有一些方便的提示(以及许多其他的智慧)可以找到
mat2=mat1+mat1
失败的方式?数组是什么样子的?你考虑到了吗?构建你自己的a,如果这不能帮助你发现问题,那么把a添加到问题中。看起来数组对象能够动态分配(可能是a
std::vector
)。如果是这样,您是否应该检查添加的
矩阵
的维度是否与您要添加的维度大小相同?也许最好将维度作为类型的一部分?“假设所有构造函数和其他代码都正确编写。”当然,我们不应该这样做。您是如何实现默认构造函数、接受
std::vector
的构造函数和副本分配的?此外,在第一个发布的代码段中,嵌套循环是行主循环(因此看起来是存储的数据),而构造函数接受维度(列、行)在另一个代码段中,嵌套循环是列主循环。在您的示例中,矩阵是方形的,因此没有任何区别,但代码应该是一致的。不
返回std::move(lhs)在这种情况下,建议不要在返回时使用
std::move
,通过plus equal运算符简化plus运算符会添加不必要的副本,应该避免使用它。@Bob_uuuu\n不,不可能NRVO函数参数。
std::move
用于提醒程序员这一事实。@Slava不可能o elide函数参数。