我試圖重載==操作符,然後用主函數中的代碼對其進行測試。它給了我一個錯誤,並說我的if語句中的z必須是bool類型或轉換爲一個。我只是想知道我在哪裏出了問題,怎麼去設置這個部分。這是代碼片段。我宣佈雙重真實&作爲私有變量也是雙重想象。C++ Overloading ==複數
Complex Complex::operator==(const Complex &operand2) const
{
if (real == operand2.real, imaginary == operand2.imaginary)
return true;
else
return false;
}
int main()
{
Complex x(1, 2);
Complex y(2, 3);
Complex z, w, v;
z = x + y;
w = x – y;
if (z == w)
cout << " z = w" << endl;
else
cout << " z != w" << endl;
return 0;
}
嗯是一個愚蠢的錯誤,完全滑倒在返回類型的我的頭腦,謝謝! – John