Iterator for pointer error vector;

I use a pointer vector to create a data structure and find that I am getting an error that seems obscure. Here is the base code from the header file

#include <vector> using namespace std; template <typename Key, typename Value> class ST{ class STNode{ public: STNode(Key k, Value v) : key(k), value(v){} ~STNode(){} Key key; Value value; }; typedef typename ST<Key, Value>::STNode Node; public: ST():v(NULL) {v = new vector<Node*>();} ~ST(){ // vector contains allocated objects for(vector<Node*>::iterator it = v->begin(); it != v->end(); ++it) delete (*it); delete v; } private: vector<Node*>* v; }; 

The error message I get in g ++ version 4.6.6 is

 ST.h: In destructor 'ST<Key, Value>::~ST()': ST.h:20: error: expected ';' before 'it' ST.h:20: error 'it' was not declared in this scope 

I tried removing the for loop and just tried declaring an iterator and getting a scope error. My searches have shown that this is usually due to a missing semicolon at the end of the inner class or a lack of public presence in the inner class, but this is not so. Is there a special declaration for a pointer vector iterator?

+4
source share
2 answers

You suffer from the interesting quirk of the C ++ language. You need to add typename to the iterator declaration ( typename vector<Node*>::iterator it ). More information can be found in the question Why do I need to use typedame typedef in g ++, but not VS?

+2
source

You need to add a typedef for vector<Node*>::iterator , since it is a dependent name , which depends on the template. Template parameter.

 for(typename vector<Node*>::iterator it = v->begin(); it != v->end(); ++it) 
+1
source

All Articles