使用 This 关键字和运算符重载

Using the This keyword and operator overload

我正在寻找有关如何在重载运算符中使用 this 关键字的一些建议。我已经尝试阅读它,但它并不完全有意义,也许有人可以更深入地解释它吗?

我还提供了我的问题所在的代码,它是为了对两个矩阵进行相加。我必须坚持 const Matrix& 论点。 我得到的当前错误是 no match for ‘operator[]’ (operand types are ‘const Matrix’ and ‘int’) 用于执行添加的 4 行代码。

int array1[2][2] = {{5, 7}, {3, 2}}; //makes the 2 dimensional arrays used to represent the matrix 
int array2[2][2] = {{2, 3}, {1, 4}};
Matrix m2(array1);
const Matrix m3(array2);

cout << m2 << " + " << m3 << " = " << m2 + m3 << endl; //here is where the operator + comes into play. (The << operator was overloaded to work with this)
cout << m3 << " + " << m2 << " = " << m3 + m2 << endl << endl;

以上所有都在主程序中找到。下面我添加了属于 .cpp 的方法和 class 定义的一些(需要的部分)。

Matrix Matrix::operator+(const Matrix& rightOp) const
{
Matrix addedMatrix;

addedMatrix.matrix[0][0] = *this->matrix + rightOp[0][0]; //current issue stands at these for lines
addedMatrix.matrix[0][1] = *this->matrix + rightOp[0][1]; //*this is used to represent the left operand of the expression, which points to the Matrix object that called the method
addedMatrix.matrix[1][0] = *this->matrix + rightOp[1][0];
addedMatrix.matrix[1][1] = *this->matrix + rightOp[1][1];

return addedMatrix;
}


Matrix::Matrix(const int newMatrix[][2]) //constructor where m2 and m3 are passed to
{
for(int row = 0; row < 2; row++)
    for(int col = 0; col < 2; col++)
        matrix[row][col] = newMatrix[row][col];  
}

ostream& operator<<(ostream& output, const Matrix& newMatrix) //overloaded << operator
{
output << "[[" << newMatrix.matrix[0][0] << ", " << newMatrix.matrix[0][1]         << "], "
           << "[" << newMatrix.matrix[1][0] << ", " << newMatrix.matrix[1][1] << "]]";

return output;
}

class Matrix //Class Defintion for Matrix
{
public:
    Matrix();
    Matrix(const int[][2]);
    Matrix operator+(const Matrix&) const;
    friend ostream& operator<<(ostream&, const Matrix&);
private:
    int matrix[2][2];
};

错误很简单,与this无关。

no match for ‘operator[]’ (operand types are ‘const Matrix’ and ‘int’)

您还没有为矩阵 class 定义 operator[],而您正试图在编写

时调用一个
rightOp[0][0]

我想你的本意是

addedMatrix.matrix[0][0] = this->matrix[0][0] + rightOp.matrix[0][0];

等等。这等同于写

addedMatrix.matrix[0][0] = matrix[0][0] + rightOp.matrix[0][0];

要获得更多帮助,您需要显示矩阵的定义 class,但对于初学者:

  • 编译器认为您正在尝试做的是访问二维矩阵数组中的元素,而我认为您想要的是访问该矩阵中的单个数字。
  • 要以这种方式使用索引运算符 ([]),您需要在矩阵中重载它们 class,在这种情况下,您不能像那样重载它们,因为索引运算符只接受一个单个整数作为参数。与重载 () 运算符相比,任何涉及多次使用索引运算符的解决方案都是复杂的,以便您可以执行 rightOp( 2, 2 ).
  • 您的 *this->matrix 应该尝试访问单个值,而不是整个矩阵。'

编辑: 查看您的代码后,您忘记的是您需要访问 Matrix.matrix[0][0],而不是 Matrix[0][0]。请记住,您实际上是从矩阵 class

内的数组中获取数字

看看here,看看解决方案是否有帮助。

为了完整性,将解决方案粘贴到此处。

Either: this->operator()(i,j) or (*this)(i,j)

operator+ 将两个参数作为输入,returns 一个新对象,而不是修改后的对象。所以将其实现为 class 成员没有意义。它 通常 作为独立的全局函数实现。另一方面,operator+= 作为 class 方法更有意义,而 operator+ 通常 根据 operator+ 实施.例如:

class Matrix
{
private:
    int matrix[2][2];

public:
    Matrix(); // initialize matrix to default values
    Matrix(const Matrix &); // copy existing matrix values
    ...

    Matrix& operator+=(const Matrix& rightOp);

    friend Matrix operator+(const Matrix& leftOp, const Matrix& rightOp);
};

Matrix operator+(const Matrix& leftOp, const Matrix& rightOp);

Matrix operator+(const Matrix& leftOp, const Matrix& rightOp)
{
    Matrix tmp(leftOp);
    tmp += rightOp;
    return tmp;
}

Matrix& Matrix::operator+=(const Matrix& rightOp)
{
    for(int row = 0; row < 2; ++row)
    {
        for(int col = 0; col < 2; ++col) {
            matrix[row][col] += rightOp.matrix[row][col];  
        }
    }
    return *this;
}

也就是说,您 可以 operator+ 实现为 class 方法:

class Matrix
{
private:
    int matrix[2][2];

public:
    Matrix(); // initialize matrix to default values
    Matrix(const Matrix &); // copy existing matrix values
    ...

    Matrix operator+(const Matrix& rightOp) const;
};

Matrix Matrix::operator+(const Matrix& rightOp) const
{
    Matrix result(*this);
    for(int row = 0; row < 2; ++row)
    {
        for(int col = 0; col < 2; ++col) {
            result.matrix[row][col] += rightOp.matrix[row][col];  
        }
    }
    return result;
}

或:

Matrix Matrix::operator+(const Matrix& rightOp) const
{
    Matrix result;
    for(int row = 0; row < 2; ++row)
    {
        for(int col = 0; col < 2; ++col) {
            result.matrix[row][col] = this->matrix[row][col] + rightOp.matrix[row][col];  
        }
    }
    return result;
}