我試圖用重載運算符+添加兩個二維矩陣時出現問題。我已經成功地將這兩個矩陣分配給了兩個不同的數組。重載運算符+ C++的錯誤
我的錯誤是:
在功能`mathadd ::矩陣mathadd ::運算符+(mathadd ::矩陣&,mathadd ::矩陣&)「:
呼叫沒有匹配功能`mathadd ::矩陣::矩陣(雙&)」
候選是:mathadd ::矩陣::矩陣(常量mathadd ::矩陣&)
在我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」:在超載操作員之前,您的問題還遠遠不夠。 – Yakk