【发布时间】:2014-02-08 18:05:09
【问题描述】:
我在尝试使用重载的 operator+ 添加两个 2D 矩阵时遇到问题。我已经成功地将这两个矩阵分配给了两个不同的数组。
我的错误是:
在函数`mathadd::matrices mathadd::operator+(mathadd::matrices&, mathadd::matrices&)'中:
没有匹配函数调用`mathadd::matrices::matrices(double&)'
候选人是:mathadd::matrices::matrices(const mathadd::matrices&)
在我的 int main() {} 中,这个错误的主要部分是:
matrices sample;
double array1[4][4], array2[4][4], add[4][4];
add[4][4] = array1[4][4] + array2[4][4];
重载的运算符定义为:
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; }
我的班级是这样的
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.
};
我不明白这个错误的含义,如果能帮助我理解它的含义以及如何解决它,我将不胜感激。
【问题讨论】:
-
在开始重载运算符之前,您需要学习基本的数组运算和基本的 C++。跑步前先学会爬行。更实际的是,编写一个函数
add,将两个矩阵相加并返回结果:您的问题远在重载运算符之前。
标签: c++ matrix multidimensional-array operator-overloading addition