std::map prob
-
class My{ int x; int y; My(int,int) }; My::My(int x2,int y2):x(x2),y(y2){} class My2{ std::map<unsigned int,My> xyz; void set(unsigned int,My); } void My2::set(unsigned int index,My myy){xyz[index] = myy;} //hier ist der fehlerfehlermeldung:
..\stl_map.h In member function `_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = unsigned int, _Tp = My, _Compare = std::less<unsigned int>, _Alloc = std::allocator<std::pair<const unsigned int, ;My> >]':..\stl_map.h no matching function for call to `My::My()'
candidates are: My::My(const My&)
My::My(int, int)
-
Hallo
Deine Klasse my braucht für die Verwendung in STL-Containern einen Standard-Konstruktor ohne Parameter und/oder einen Kopierkonstruktor.
bis bald
akari
-
Und verwende am besten eine konstante Referenz für deine Parameter:
void My2::set(unsigned int index, const My &myy) { .. }So braucht das Objekt nicht kopiert zu werden.
-
akari schrieb:
Hallo
Deine Klasse my braucht für die Verwendung in STL-Containern einen Standard-Konstruktor ohne Parameter und/oder einen Kopierkonstruktor.
bis bald
akariDas ist nicht richtig. Ein Typ für einen Container muss copy-konstruierbar und zuweisbar sein.
The type of objects stored in these components must meet the requirements of CopyConstructible
types (20.1.3), and the additional requirements of Assignable types.Man braucht für map::operator[] einen Defaultkonstruktor (auch hier stimmt nicht, dass er ohne Parameter sein muss), da er folgende Semantik hat:
return (*((insert(make_pair(x, T()))).first)).second;
-
das hilft mir aber nicht weiter

-
Wenn deine STL-Implementierung wirklich einen Default-Konstruktor braucht, dann kannst du einfach Default-Parameter angeben:
class My { public: My(int =0,int =0); };
-
Th schrieb:
Wenn deine STL-Implementierung wirklich einen Default-Konstruktor braucht, dann kannst du einfach Default-Parameter angeben:
class My { public: My(int =0,int =0); };map<> an sich braucht keinen - ihr Index-Operator schon (wenn der angegebene Schlüssel noch nicht vorkommt, wird er - mit default-konstruiertem Wert - neu eingefügt). Also entweder du verpasst deiner Klasse einen Ctor, der ohne Parameter verwendet werden kann oder du verwendest den Index-Operator nicht:
void My2::set(unsigned ind, const My& val) { map<unsigned,My>::iterator pos = xyz.find(ind); if(pos==xyz.end()) xyz.insert(make_pair(ind,val)); else pos->second = val; }(ist etwas umfangreicher, kommt aber ohne Default-Ctor aus)