[英]Error with overloaded operator+ C++
I'm having a problem trying to add two 2D matrices with the overloaded operator+. 尝试使用重载的operator +添加两个2D矩阵时遇到问题。 I've successfully assigned the two matrices to two different arrays.
我已经成功地将两个矩阵分配给了两个不同的数组。
My errors are: 我的错误是:
In function `mathadd::matrices mathadd::operator+(mathadd::matrices&, mathadd::matrices&)':
在函数`mathadd :: matrices mathadd :: operator +(mathadd :: matrices&,mathadd :: matrices&)'中:
no matching function for call to `mathadd::matrices::matrices(double&)'
没有匹配的函数来调用`mathadd :: matrices :: matrices(double&)'
candidates are: mathadd::matrices::matrices(const mathadd::matrices&)
候选对象是:mathadd :: matrices :: matrices(const mathadd :: matrices&)
In my int main() {} the main parts of this error are: 在我的int main(){}中,此错误的主要部分是:
matrices sample;
double array1[4][4], array2[4][4], add[4][4];
add[4][4] = array1[4][4] + array2[4][4];
The overloaded operator definition is: 重载的运算符定义为:
matrices operator +(matrices& p1, matrices& p2) { double addition[4][4]; for (int y = 0; y < 4; ++y ) { for (int x = 0; x < 4; ++x ) { addition[4][4] = p1.get_array1() + p2.get_array2(); } } matrices sum(addition[4][4]); // This is where the error is. return sum; }
My class looks like this 我的班级看起来像这样
class matrices
{
public:
matrices();
void assignment(double one[4][4], double two[4][4]);
double get_array1() const {return first_array[4][4];}
double get_array2() const {return second_array[4][4];}
matrices operator +(matrices& p1, matrices& p2);
private:
double first_array[4][4], second_array[4][4];
//Initialized to 0 in constructor.
};
I don't understand what this error means and I would appreciate any help in understanding what it means and how I can fix it. 我不了解此错误的含义,希望能帮助您理解此错误以及如何解决。
addition[4][4]
is a double out-of-bounds array access to get the fifth double
from the fifth double[]
named by addition
. addition[4][4]
是双外的边界数组访问,以获得第五double
从第五double[]
由名为addition
。 Just pass the name addition
instead of addition[4][4]
in matrices sum(addition[4][4])
. 只是通过名称
addition
代替addition[4][4]
中matrices sum(addition[4][4])
So it should be 所以应该
matrices sum(addition);
return sum;
That is only the source of your compiler error. 那仅仅是编译器错误的根源。 You also have many logic errors in your code, such as the out-of-bounds array access I mentioned earlier, in the inner
for
-loop. 您的代码中还存在许多逻辑错误,例如前面提到的
for
-loop内部的越界数组访问。 You will have to fix these or else get undefined behavior. 您将不得不修复这些问题,否则将获得未定义的行为。
You get an error because your operator +
is defined for objects of type matrices
, not for 2D arrays of double
s. 您会收到错误消息,因为您的
operator +
是为matrices
类型的对象定义的,而不是为double
s的2D数组定义的。 You need to construct matrices before adding them, like this: 您需要在添加矩阵之前先构建矩阵,如下所示:
First, add a constructor for matrices
that takes double[4][4]
. 首先,为采用
double[4][4]
matrices
添加一个构造函数。 Then, change the signature of the operator +
to be static
, and take a const
reference to its parameters: 然后,将
operator +
的签名更改为static
,并对其参数采用const
引用:
static matrices operator +(const matrices& p1, const matrices& p2);
Now you can write this: 现在,您可以编写以下代码:
matrices add = matrices(array1) + matrices(array2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.