简体   繁体   English

加法运算符重载不起作用

[英]Addition Operator Overloading Not Working

I am having trouble getting my addition overloading function too work properly and was wondering if I could get some help with it. 我无法让我的附加重载功能正常工作,并且想知道是否可以得到一些帮助。 The rest of the functions and constructors in the class are default and can not be changed for this project so regardless they are correct. 该类中的其余函数和构造函数都是默认的,因此无法更改此项目,因此无论它们是否正确。 This means the only problems I am having are with the operator overloading functions themselves. 这意味着我唯一的问题在于操作员重载函数本身。 Thanks in advance. 提前致谢。

Matrix.h: Matrix.h:

class Matrix {
// models a matrix of two dimentions, i. e. rows and columns of values

public:
    Matrix & operator=(const Matrix& m);

    Matrix & operator+(const Matrix& m);

Matrix.cpp: Matrix.cpp:

//not included are the default and copy constructors plus read and write    
//functions, etc...
Matrix& Matrix::operator=(const Matrix& m) {
    this->rows = m.rows;
    this->cols = m.cols;
    matrix = vector< vector<double> >(rows);
    for (int r=0; r<rows; r++)
        matrix[r] = vector<double>(cols);
    for (int r=0; r<rows; r++)
        for (int c=0; c<cols; c++)
            matrix[r][c] = m.matrix[r][c];
    return *this;
}

Matrix & Matrix::operator+(const Matrix& m) {
    Matrix newMatrix;
    newMatrix = vector< vector<double> >(rows);
    if (this->rows != m.rows || this->cols != m.cols) {
        newMatrix.rows = 0;
        newMatrix.cols = 0;

        return newMatrix;
    }
    else {
        newMatrix.rows = m.rows;
        newMatrix.cols = m.cols;
        for (int r = 0; r < m.rows; r++) {
            newMatrix.matrix[r] = vector<double>(m.cols);
        }

        for (int r = 0; r < m.rows; r++) {
            for (int c = 0; c < m.cols; c++)
                newMatrix.matrix[r][c] = matrix[r][c] + m.matrix[r][c];
        }
        return newMatrix;
    }
}

Main.cpp: Main.cpp的:

//in main.cpp I am trying to do the following operation and then output the result:
e = a + b;

While assignment operators should return a reference to the left object (resp. *this ), all other infix operators should return by value. 赋值运算符应返回对左对象的引用(分别是*this ),而所有其他中缀运算符应按值返回。 They are most easily written using the respective compound-assignment operator like += . 使用相应的复合赋值运算符(如+=最容易编写它们。
For those reason most infix non-assignment-operators are free functions. 由于这些原因,大多数infix非分配运算符都是自由函数。

General pattern for infix +: 前缀+的一般模式:

template<typename T> inline const T operator(const T& a, const T& b) {
    T c = a;
    return c += b;
}

You do not want to have such infix-operators as member-functions, because that breaks symmetry. 您不想拥有像成员函数这样的中缀运算符,因为这样会破坏对称性。
Instead, just define the compound operator += . 相反,只需定义复合运算符+=

You're returning a reference to the temporary newMatrix 您正在返回对临时newMatrix的引用

Instead, your addition operator should return the result by value. 相反,您的加法运算符应按值返回结果。

The function signature should look like this: 函数签名应如下所示:

Matrix operator+(const Matrix& m);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM