views:

469

answers:

2

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(...)

+13  A: 

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.

anon
Looks like..Its amazing how many questions on the missing typename exists in SO.
Naveen
Boy, this sure is turning out to be a common problem.
Bobby Alexander
We met last time on my question for which the answer was *ding* *ding* typename! ;)
Bobby Alexander
+1, This sounds like : many see little veeem.
lsalamon
Well I did try to find errors like this one before I asked, but g++ doesn't exactly give good errors. I've never actually learn c++, I just use what I've picked up along the way.
KitsuneYMG
+1 for heuristic ;)
aJ
@kts: I don't think you could be expected to find an answer by searching - you'd pretty much have to know the answer to come up with a reasonable search query.
Michael Burr
If the same person had answered all the SO questions related to the typename issue, I think he would easily beat Jon Skeet's rep! :-)
Luc Touraille
+2  A: 

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.

jmanning2k