Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
};
like image 408
Joe Avatar asked Jan 05 '14 13:01

Joe


1 Answers

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;
like image 79
juanchopanza Avatar answered Oct 16 '22 04:10

juanchopanza