Compiler-Fehlermeldung unklar
-
Ich habe folgendes geschrieben
class ConnectionKey { public: uint32_t serverIP; uint32_t clientIP; ConnectionKey( uint32_t serverIP, uint32_t clientIP ); bool operator==(ConnectionKey); }; ConnectionKey::ConnectionKey( uint32_t serverIP_, uint32_t clientIP_ ) { serverIP = serverIP_; clientIP = clientIP_; } bool ConnectionKey::operator==( ConnectionKey& connKey ) { if( connKey.serverIP == serverIP && connKey.clientIP == clientIP ) return true; return false; } typedef boost::unordered_map<ConnectionKey, TCPConnection, boost::hash<ConnectionKey> > tcpConnectionsMap; tcpConnectionsMap tcpConnections; //weiter im Code... if( tcpConnections.find( connKey ) == tcpConnections.end() ) { <-- Zeile 172 ...Wenn ich das nun kompiliere, dann er halte ich folgende Fehlerausgabe:
ddos_main.cpp:172: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h:200: error: passing ‘const ConnectionKey’ as ‘this’ argument of ‘bool ConnectionKey::operator==(ConnectionKey)’ discards qualifiersWas mache ich falsch?
-
bool operator==(ConnectionKey)const; // <--- const einfügen
-
bool operator==(const ConnectionKey& ) const;Besser wäre auch, den Parameter als Referenz zu übergeben, hat aber mit dem Fehler nichts zu tun.
Lars
-
Und
if (x) return true; return false;sollte man auch besser gleich durch
return x;ersetzen.
-
Ich würde den Vergleichsoperator auch als freie Funktion implementieren statt als member.
-
Danke für die Hinweise.
-
Verzeih die Werbung - aber der Artikel zur Operatorüberladung im Magazin scheint einigen geholfen zu haben: http://magazin.c-plusplus.net/artikel/�berladung von Operatoren in CPlusPlus (Teil 1)