2010-07-09 7 views
9

Sto usando gcc 4.3.3 per cercare di compilare il codice seguente:nessuna corrispondenza per "operatore <" quando si tenta di inserire un set (C++)?

struct testStruct { 
int x; 
int y; 
bool operator<(testStruct &other) { return x < other.x; } 
testStruct(int x_, int y_) { 
    x = x_; 
    y = y_; 
} 
}; 


int main() { 
multiset<testStruct> setti; 
setti.insert(testStruct(10,10)); 
return 0; 
} 

ottengo questo errore:
/usr/include/c++/4.4/bits/stl_function.h|230|error : nessuna corrispondenza per 'operatore <' in '__x < __y'
Sospetto che non stia facendo l'overloading dell'operatore come dovrebbe, ma non riesco a individuare esattamente il problema. Cosa sto facendo di sbagliato qui?

risposta

13

L'operatore deve essere const e prendere un riferimento const:

bool operator<(const testStruct &other) const { return x < other.x; } 
+0

Grazie, che risolto. – tsiki

+0

+1 per il metodo const. –

Problemi correlati