2012-04-26 36 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; 
} 

我有上面的代碼和編譯器引發不可理解的錯誤。我不知道如何重載結構中的==運算符。在結構體中重載double等於運算符?

回答

3

添加一對括號的:

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

否則這被分析爲:

(cout << xy1) == xy2 
+0

愚蠢的愚蠢的錯誤。謝謝。 – tree 2012-04-26 05:10:59