Iteratorenproblem...
-
Hi... hab irgendwie ein dummes Iteratorenproblem:
typedef vector<Command*> CommandVector; typedef CommandVector::iterator CommandIter; typedef map<Uint8, CommandVector>::const_iterator ConstMapIter; /..... void SdlCommandEmitter::dispatchKeyEvents(map<Uint8, CommandVector> keyMap) { ConstMapIter it = keyMap.find(event.key.keysym.sym); if (it != keyMap.end()) { // die naechsten Zeilen geben Fehler ( == Zeile 70 und 71) for(CommandIter command = it->second->begin(); command != it->second->end(); ++command) { emitCommand(*command); } } }
Und der GCC (3.3.1) sagt dazu
src/input/SdlCommandEmitter.cpp: In member function `void SdlCommandEmitter::dispatchKeyEvents(std::map<Uint8, std::vector<Command*, std::allocator<Command*> >, std::less<Uint8>, std::allocator<std::pair<const Uint8, std::vector<Command*, std::allocator<Command*> > > > >)': src/input/SdlCommandEmitter.cpp:70: error: base operand of `->' has non-pointer type `const std::vector<Command*, std::allocator<Command*> >' src/input/SdlCommandEmitter.cpp:71: error: no match for 'operator!=' in ' command != (&std::vector<_Tp, _Alloc>::end() const [with _Tp = Command*, _Alloc = std::allocator<Command*>]())->__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator*() const [with _Iterator = Command* const*, _Container = std::vector<Command*, std::allocator<Command*> >]()'
Hab ich was uebersehen?
it->second
muesste einen std::vector zurueckliefern, und
it->second->begin()
somit einen std::vector<>::iterator. Warum geht das nicht?
-
Blue-Tiger schrieb:
Hab ich was uebersehen?
it->second
muesste einen std::vector zurueckliefern, und
it->second->begin()
Soweit ich mich erinnere kann man auf die Methoden eines std::vector mit ., nicht mit -> zugreifen:
it->second.begin()
MfG Jester
-
herzlichen Dank... Aber trotzdem noch ein Problem:
Die Zeile
for(CommandIter command = it->second.begin(); command != it->second.end(); ++command)
liefert jetzt:
d:/dev/Dev-Cpp/include/c++/3.3.1/bits/stl_iterator.h: In constructor ` __gnu_cxx::__normal_iterator<_Iterator, _Container>::__normal_iterator(const __gnu_cxx::__normal_iterator<_Iter, _Container>&) [with _Iter = Command* const*, _Iterator = Command**, _Container = std::vector<Command*, std::allocator<Command*> >]': src/input/SdlCommandEmitter.cpp:70: instantiated from here d:/dev/Dev-Cpp/include/c++/3.3.1/bits/stl_iterator.h:598: error: invalid conversion from `Command* const* const' to `Command**'
-
Das Problem liegt daran, daß Du einen const_iterator benutzt um über Deine map zu iterieren. Das heißt jeder der Vektoren in der map ist dann für Dich const, weil Du über den const_iterator zugreifst. Wenn Du jetzt dort begin()/end() aufrufst, wird die const-Überladung ausgewählt. Diese liefert einen const_iterator, den wiederum versuchst Du einem iterator zuzuweisen, was nicht geht.
MfG Jester
-
Danke vielmals