在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;
};
答案 0 :(得分:23)
std::vector<T>::iterator
是dependent name,因此您需要typename
来指定它引用的类型。否则,假设它引用非类型:
typedef typename std::vector<T>::iterator iterator;