Tutorial :C++ iterator problems


I have the following member data

vector<State<T>*> activeChildren;  

I want to clean-up these pointers in my destructor

StateContainer<T>::~StateContainer() {      vector<State<T>*>::iterator it =          activeChildren.begin();      while(it!=activeChildren.end()) {          State<T>* ptr = *it;          it = activeChildren.erase(it);          delete ptr;      }  }  

I get the following error from g++ 4.3.2 on Ubuntu:

./fsm2/StateContainer.cpp: In destructor ‘virtual ervan::StateContainer<T>::~StateContainer()’:  ../fsm2/StateContainer.cpp:24: error: expected `;' before ‘it’  ../fsm2/StateContainer.cpp:25: error: ‘it’ was not declared in this scope  

Can anyone tell me what I've done wrong? I get this error in two more places where I use iterator loops, but not when I use for_each(...)


Looks like typename time again - I think you need:

typename vector<State<T>*>::iterator it = ...  

A heuristic for g++ users - when you see this message in template code:

expected `;' before ‘it’  

it is a pretty good bet that the thing in front of the 'it' is not being seen by the compiler as a type and so needs a 'typename' added.


It's a parsing issue. In this code, vector<State<T>*>::iterator is a nested dependent type.

Until you know what T is (and T isn't known at parse time), the parser/compiler doesn't realize that iterator is a type (could be a static member variable, for all it knows).

Therefore, you must prefix the definition with typename as a hint to tell the compiler that for all vector<State<T>*>, vector<State<T>*>::iterator is a type name.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »