Class 运算符+;=类重载:参数错误
主文件:Class 运算符+;=类重载:参数错误,class,operator-overloading,overloading,Class,Operator Overloading,Overloading,主文件: class Matrix2x2 { private: double anArray[2][2]; public: Matrix2x2(); Matrix2x2(int a, int b, int c, int d); void setArrayValues(int row, int column, double value); const double getArrayValues(int row, int column) const;
class Matrix2x2
{
private:
double anArray[2][2];
public:
Matrix2x2();
Matrix2x2(int a, int b, int c, int d);
void setArrayValues(int row, int column, double value);
const double getArrayValues(int row, int column) const;
Matrix2x2& operator +=(const Matrix2x2 & rhs)
};
当我尝试运行此操作时,我得到以下结果:
错误:“Matrix2x2&operator+=(const matrix2x2x2&)”必须正好包含两个参数
我不明白为什么
我把它换成了
Matrix2x2 aMatrix(4,4,4,4);
Matrix2x2 bMatrix;
aMatrix += bMatrix;
然后我得到了这些错误:
Matrix2x2& Matrix2x2::operator+=(const Matrix2x2 & rhs);
在头文件和
error: extra qualification 'Matrix2x2::' on member 'operator+='
在这方面:
error: expected unqualified-id before '[' token|
更新2
我已经向您展示了头文件中的类声明、主文件中的调用以及cpp文件中的函数定义。还有什么可以展示的?我现在已经纠正了你指出的所有错误,但我仍然得到了一个相同的错误:
this->anArray.[i][n] += rhs.anArray[i][n];
在cpp和头文件中。确定,存在大量问题 首先,在头文件中,此行需要位于
matrix2x2x2
的类声明中(最好位于public:
标签下)
其次,您需要将您的定义放入CPP文件中,它需要如下所示:
Matrix2x2& operator +=(const Matrix2x2 & rhs);
致:
我不能保证没有更多的问题。但是这些是我根据你给我们展示的内容看到的。请展示你如何为你的对象调用
+=
。那不是应该是matrix2x2x2&matrix2x2x2x2::operator+=
…在我的类中它被定义为:Matrix2x2&operator+=(const matrix2x2x2&rhs);我还没有尝试调用+=操作符,我只是编写了函数。这可能与此有关吗?如果我尝试用Matrix2x2&matrix2x2x2::operator+=替换它,我会得到以下结果:错误:额外限定'matrix2x2x2::'on member'operator+='并且:在此行中的'['标记前面应该有非限定id:this->anArray.[I][n]+=rhs.anArray[I][n];@user1784297,那么,在这种情况下,正如许多人所说,我们需要看到更多的代码。除非您首先确认该请求,否则不要回复。请您解释一下,this->anArray是什么意思。[i][n]
的意思是什么?(我是关于那里的点)。我用“this”引用调用对象,并使用->作为成员数组“anArray”的指针.@user1784297,语法anArray.[i][n]
是一个错误。请按照我向您展示的方式进行更改
error: extra qualification 'Matrix2x2::' on member 'operator+=
Matrix2x2& operator +=(const Matrix2x2 & rhs);
Matrix2x2& Matrix2x2::operator+=(const Matrix2x2 & rhs)
{
for (int i = 0; i < 2; i++)
{
for (int n = 0; n < 2; n++)
{
this->anArray[i][n] += rhs.anArray[i][n];
}
}
return *this;
}
this->anArray.[i][n] += rhs.anArray[i][n];
this->anArray[i][n] += rhs.anArray[i][n];