error C2678 bei Operatorenüberladung in MSVC Express 2005
-
Ich bekomme einen error C2678 bei einer Operatorenüberladung. Über Internet-postings habe ich gelesen, es sein ein compiler-spezifischer error, daher poste ich hier.
Der code:static struct ErrorEntry { int nID; const char* pcMessage; ErrorEntry(int id, const char* pc = 0) : nID(id), pcMessage(pc) { } bool operator<(const ErrorEntry& rhs) { return nID < rhs.nID; } };Die Fehlermeldung lautet:
c:\programme\microsoft visual studio 8\vc\include\xutility(267) : error C2678: Binärer Operator '<': Es konnte kein Operator gefunden werden, der einen linksseitigen Operanden vom Typ 'const ErrorEntry' akzeptiert (oder keine geeignete Konvertierung möglich) (...)\ws-util.cpp(45): kann 'bool ErrorEntry::operator <(const ErrorEntry &)' sein bei Anpassung der Argumentliste '(const ErrorEntry, ErrorEntry)' c:\programme\microsoft visual studio 8\vc\include\algorithm(1992): Siehe Verweis auf die Instanziierung der gerade kompilierten Funktions-template "bool std::_Debug_lt<ErrorEntry,_Ty>(_Ty1 &,const _Ty2 &,const wchar_t *,unsigned int)". with [ _Ty=ErrorEntry, _Ty1=ErrorEntry, _Ty2=ErrorEntry ] c:\programme\microsoft visual studio 8\vc\include\algorithm(2004): Siehe Verweis auf die Instanziierung der gerade kompilierten Funktions-template "_FwdIt std::_Lower_bound<ErrorEntry*,_Ty,std::iterator_traits<_Iter>::difference_type>(_FwdIt,_FwdIt,const _Ty &,_Diff *)". with [ _FwdIt=ErrorEntry *, _Ty=ErrorEntry, _Iter=ErrorEntry *, _Diff=std::iterator_traits<ErrorEntry *>::difference_type ] (...)\ws-util.cpp(128): Siehe Verweis auf die Instanziierung der gerade kompilierten Funktions-template "_FwdIt std::lower_bound<ErrorEntry*,ErrorEntry>(_FwdIt,_FwdIt,const _Ty &)". with [ _FwdIt=ErrorEntry *, _Ty=ErrorEntry ]
-
Erstens ist das kein Compiler-spezifisches Problem, sondern tritt vermutlich überall auf (mit unterschiedlichen Fehlercodes).
Zweitens zur Lösung:
Dein operator< kann nur angewendet werden, wenn auf der linken Seite ein nicht-konstantes Objekt steht. Allerdings hast du an der Stelle, wo er aufgerufen werden soll, nur ein "const ErrorEntry" zur Verfügung. Da der Operator nicht vorhat, etwas an this zu ändern, kannst du ihn aber problemlos als const deklarieren, was dein Problem lösen sollte:static struct ErrorEntry { int nID; const char* pcMessage; ErrorEntry(int id, const char* pc = 0) : nID(id), pcMessage(pc) { } bool operator<(const ErrorEntry& rhs) /*Das ist wichtig:*/ const { return nID < rhs.nID; } };
-
Dieser Thread wurde von Moderator/in CStoll aus dem Forum MFC (Visual C++) in das Forum C++ verschoben.
Im Zweifelsfall bitte auch folgende Hinweise beachten:
C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?Dieses Posting wurde automatisch erzeugt.
-
Vielen Dank für Deine Hilfe, Problem ist gelöst!