C ++模板std :: vector :: iterator错误

时间:2014-01-05 13:48:58

标签: c++ templates vector iterator

在C ++中,我试图为我的模板类获得std::vector::iterator。但是,当我编译它时,我收到错误:error C2146: syntax error : missing ';' before identifier 'iterator'error C4430: missing type specifier - int assumed. Note: C++ does not support default-int。我也收到警告: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)
};

我甚至尝试过

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

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

1 个答案:

答案 0 :(得分:23)

std::vector<T>::iteratordependent name,因此您需要typename来指定它引用的类型。否则,假设它引用非类型:

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