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

Joe*_*Joe 9 c++ templates iterator vector

在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)
};
Run Code Online (Sandbox Code Playgroud)

我甚至试过了

template<typename T> class v1{
    typedef std::vector<T>::iterator iterator;
};
Run Code Online (Sandbox Code Playgroud)

template<typename T = int> class v1{
    typedef std::vector<T>::iterator iterator;
};
Run Code Online (Sandbox Code Playgroud)

jua*_*nza 25

std::vector<T>::iterator是一个从属名称,所以你需要在typename这里指定它引用一个类型.否则假定引用非类型:

typedef typename std::vector<T>::iterator iterator;
Run Code Online (Sandbox Code Playgroud)