【发布时间】:2011-09-07 21:50:42
【问题描述】:
我一直在实现一个自定义模板矩阵类,我有一个需要帮助的函数。我正在尝试重载 operator+= ,为此我使用了我已经实现并正在工作的重载 operator[] 。问题是,我不知道如何将 'this' 指针与 operator[] 合并。
这就是我想要做的:
Matrix & operator+= (const Matrix & rhs)
{
if(this->numrows() != rhs.numrows() || this->numcols() != rhs.numrows())
{
cout << "ERR0R: Cannot add matrices of different dimensions." << endl;
return *this;
}
else
{
theType temp1, temp2, temp3;
for(int i = 0; i < this->numrows(); i++)
{
for(int j = 0; j < this->numcols(); j++)
{
temp1 = this->[i][j];
temp2 = rhs[i][j];
temp3 = temp1 + temp2;
this->[i][j] = temp3;
}
}
return *this;
}
}
不管我的错误/业余/冗余编码如何,:P 我主要关心的是如何像我所说的“rhs[i][j]”一样使用“this”指针。 (因为 this->[i][j] 或 this.[i][j] 都不起作用)
我在想也许它会长期有效operator[] (i) >> 但我不知道如何将双括号合并到其中。或者也许完全有另一种选择。我希望我能很好地解释自己。我感觉答案很简单。我只是难过。任何帮助表示赞赏。
谢谢。
【问题讨论】: