Joe
Joe

Reputation: 1089

C++ template std::vector::iterator error

In C++, I am trying to get a std::vector::iterator for my templated class. However, when I compile it, I get the errors: error C2146: syntax error : missing ';' before identifier 'iterator', error C4430: missing type specifier - int assumed. Note: C++ does not support default-int. I also get the warning: warning C4346: 'std::vector<T>::iterator' : dependent name is not a type:

#include <vector>
template<class T> class v1{
    typedef std::vector<T>::iterator iterator; // Error here
};
class v2{
    typedef std::vector<int>::iterator iterator; // (This works)
};

I have even tried

template<typename T> class v1{
    typedef std::vector<T>::iterator iterator;
};

And

template<typename T = int> class v1{
    typedef std::vector<T>::iterator iterator;
};

Upvotes: 15

Views: 17179

Answers (1)

juanchopanza
juanchopanza

Reputation: 227390

std::vector<T>::iterator is a dependent name, so you need a typename here to specify that it refers to a type. Otherwise it is assumed to refer to a non-type:

typedef typename std::vector<T>::iterator iterator;

Upvotes: 34

Related Questions