Probleme bei Operator-Überladung von <
-
Hi
Ich habe schon länger kein operator overloading mehr gebraucht, aber nun ist es wieder so weit. Kann mir jemand sagen, was an dem Code falsch ist?
struct Node { Node* left; Node* right; std::string symbol; Node( std::string & a_symbol, Node* a_left = 0, Node* a_right = 0 ) : left(a_left), right(a_right), symbol(a_symbol) {} bool operator< ( const Node & lhs, const Node & rhs ) { return false; } };Ich bekomme folgende Fehlermeldung:
error C2804: binary 'operator <' has too many parameters
error C2333: '`anonymous-namespace'::Node::operator <' : error in function declaration; skipping function bodyIch habe extra schon im Beitrag von pumuckl über overloading nachgesehen. Es müsste aus meiner Sicht stimmen was ich mache.
Ich hoffe, ich übersehe nichts peinliches

-
Wenn du den Operator als Methode der Klasse/Struct definierst, benötigst du nur einen Parameter (der linke Operand ist *this). Zwei Parameter benötigst du nur, wenn du den Operator global, d.h. außerhalb der Klasse, definierst.
-
Stimmt. Die Funktion wird ja auf dem Objekt aufgerufen.
Das Problem ist nur, dass ich auch mit
bool operator< ( const Node & lhs ) { return false; }folgenden Fehler erhalte:
c:\program files\microsoft visual studio 10.0\vc\include\algorithm(3559): error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'ica::Letter'
c:\program files\microsoft visual studio 10.0\vc\include\string(243) : see declaration of 'std::operator <'
c:\program files\microsoft visual studio 10.0\vc\include\algorithm(3614) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2> std::_Unguarded_partition<_RanIt>(_RanIt,_RanIt)' being compiled
with
[
_Ty1=ica::Letter *,
_Ty2=ica::Letter ,
_RanIt=ica::Letter *
]
c:\program files\microsoft visual studio 10.0\vc\include\algorithm(3642) : see reference to function template instantiation 'void std::_Sort<ica::Letter,__w64 int>(_RanIt,_RanIt,_Diff)' being compiled
with
[
_RanIt=ica::Letter *,
_Diff=__w64 int
]
c:\users\urs\documents\eth\2. semester\datenstrukturen und algorithmen\serie 10\huffman code\huffman code\huffmancode.cpp(10) : see reference to function template instantiation 'void std::sort<std::_Vector_iterator<_Myvec>>(_RanIt,_RanIt)' being compiled
with
[
_Myvec=std::_Vector_val<ica::Letter,std::allocatorica::Letter>,
_RanIt=std::_Vector_iterator<std::_Vector_val<ica::Letter,std::allocatorica::Letter>>
]
c:\program files\microsoft visual studio 10.0\vc\include\algorithm(3559): error C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'ica::Letter'
c:\program files\microsoft visual studio 10.0\vc\include\string(233) : see declaration of 'std::operator <'
c:\program files\microsoft visual studio 10.0\vc\include\algorithm(3559): error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'ica::Letter'
c:\program files\microsoft visual studio 10.0\vc\include\string(223) : see declaration of 'std::operator <'
c:\program files\microsoft visual studio 10.0\vc\include\algorithm(3559): error C2784: 'bool std::operator <(const std::vector<_Ty,_Ax> &,const std::vector<_Ty,_Ax> &)' : could not deduce template argument for 'const std::vector<_Ty,_Ax> &' from 'ica::Letter'
c:\program files\microsoft visual studio 10.0\vc\include\vector(1502) : see declaration of 'std::operator <'
c:\program files\microsoft visual studio 10.0\vc\include\algorithm(3559): error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : could not deduce template argument for 'const std::unique_ptr<_Ty,_Dx> &' from 'ica::Letter'
c:\program files\microsoft visual studio 10.0\vc\include\memory(2582) : see declaration of 'std::operator <'
c:\program files\microsoft visual studio 10.0\vc\include\algorithm(3559): error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'ica::Letter'
c:\program files\microsoft visual studio 10.0\vc\include\xutility(1356) : see declaration of 'std::operator <'
c:\program files\microsoft visual studio 10.0\vc\include\algorithm(3559): error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'ica::Letter'
c:\program files\microsoft visual studio 10.0\vc\include\xutility(1179) : see declaration of 'std::operator <'
c:\program files\microsoft visual studio 10.0\vc\include\algorithm(3559): error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'ica::Letter'
c:\program files\microsoft visual studio 10.0\vc\include\utility(318) : see declaration of 'std::operator <'
c:\program files\microsoft visual studio 10.0\vc\include\algorithm(3559): error C2676: binary '<' : 'ica::Letter' does not define this operator or a conversion to a type acceptable to the predefined operator
c:\program files\microsoft visual studio 10.0\vc\include\algorithm(3559): fatal error C1903: unable to recover from previous error(s); stopping compilationKannst du mir sagen was ich falsch mache?
*Edit
Ich glaube es müsste so aussehen:bool operator< ( const Node & lhs ) const { return false; }Bekomme aber immer noch den gleichen Fehler.
-
Was ist denn ica::Letter? Das sieht auf jeden Fall nicht nach einem vordefinierten Typ aus der Standard-Bibliothek aus.
Außerdem solltest du mal die Stelle posten, die diesen Fehler verursacht hat.
-
Da der Code noch sehr kurz ist werde ich ihn mal ganz posten (ist natürlich noch nicht fertig und ich noch nicht wirklich alles sorgfältig überdacht. Ist mehr um ein bisschen zu testen gedacht):
//HuffmanCode.h #ifndef HUFF_44_MAN_CODE #define HUFF_44_MAN_CODE #include <iostream> #include <vector> #include <string> namespace { struct Node { Node* left; Node* right; std::string symbol; Node( std::string & a_symbol, Node* a_left = 0, Node* a_right = 0 ) : left(a_left), right(a_right), symbol(a_symbol) {} bool operator< ( const Node & lhs ) const { return false; } }; } namespace ica { struct Letter { char letter; int frequency; }; class HuffmanCode { private: std::vector<Letter> symbols; Node* root; public: // Ctor HuffmanCode( std::vector<Letter> & a_symbols ); //std::string encode( const std::string & s ); //std::string decode( const std::string & s ); }; } #endifUnd das Implementierungsfile:
#include "HuffmanCode.h" #include <algorithm> namespace ica { HuffmanCode::HuffmanCode( std::vector<Letter> & a_symbols ) : symbols(a_symbols), root(0) { std::sort( symbols.begin(), symbols.end() ); // Hier liegt das Problem und ist auch der Grund, warum ich den '<' Operator überladen muss. } }
-
namespace { struct Node { Node* left; Node* right; std::string symbol; Node( std::string & a_symbol, Node* a_left = 0, Node* a_right = 0 ) : left(a_left), right(a_right), symbol(a_symbol) {} }; bool operator< ( const Node & lhs, const Node& rhs) const { return false; } }std::sort braucht die 2 wertige Variante.
-
Oder alternativ einen less-Funktor, den es bereits gibt. Dann sollte es auch mit der anderen Variante gehen.
-
@drakon
Danke. Ich habs jetzt gemacht, aber etwas scheint noch immer nicht ganz zu stimmen. Ich werds mir dann morgen nochmals genauer anschauen.@ /rant/
Meinst du einen einen Zeiger auf eine Funktion, die den Vergleich macht, als dritten Parameter zu übergeben?
-
Das liegt dann wahrscheinlich wo anders.

Er meint damit ein Funktionsobjekt.
-
drakon schrieb:
std::sort braucht die 2 wertige Variante.
Nein, sort() ist es egal, wie der Vergleichsoperator definiert ist, solange es dort die sortierten Objekte darin übergeben kann.
Und genau da liegt das Problem - du benötigst einen Vergleichs-Operator für ica::Letter - oder eventuell eine Typumwandlung von ica::Letter nach Node (so daß dein operator anwendbar wird).
-
@ Drakon
Achso, hatte den Begriff Funktor vergessen. Funktionsobjekt ist klar
@ CStoll
Ich könnte mich gerade Ohrfeigen.
Wenn ich einen vector<Letter> sortieren will, dann sollte ich den '<'-Operator auch in Letter und nicht in Node überladen
Ich habe den Operator jetzt in Letter überladen und alles funktioniert.Naja, ich habe sehr wenig geschlafen diese Woche ^^
Danke für eure Hilfe.
-
CStoll schrieb:
drakon schrieb:
std::sort braucht die 2 wertige Variante.
Nein, sort() ist es egal, wie der Vergleichsoperator definiert ist, solange es dort die sortierten Objekte darin übergeben kann.
Stimmt. Hab nur zur Sicherheit mal kurz hier geschaut und nur auf das comp Argument geschaut..
Ich geh auch besser schlafen. ^^
-
Wieso schreiben alle C++ in eine .h-Datei?

-
EOutOfResources schrieb:
Wieso schreiben alle C++ in eine .h-Datei?

Gewohnheit
