2012-04-26 14 views
1
#include <iostream> 
using namespace std; 

struct coord { 
int x; 
int y; 
    bool operator== (const coord &c1) { 
    return (x == c1.x && y == c1.y); 
    } 
}; 

int main() { 
coord xy1 = {12, 20}; 
coord xy2 = {12, 20}; 
cout << xy1 == xy2 << endl; 
return 0; 
} 

私は上記のコードを持っており、コンパイラは理解できないエラーを投げています。私はかなり構造体の==演算子をオーバーロードする方法を理解できません。構造体の二重等価演算子をオーバーロードしますか?

答えて

3

は括弧のペアを追加します。

(cout << xy1) == xy2 
+0

愚かな愚かな間違い:

cout << (xy1 == xy2) << endl; 

は、そうでない場合は、このは、次のように解析されます。ありがとうございました。 – tree

関連する問題