在 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;
};
最佳答案
std::vector<T>::iterator
是 dependent name , 所以你需要一个 typename
这里指定它指的是一个类型。否则它被假定为引用一个非类型:
typedef typename std::vector<T>::iterator iterator;
关于C++ 模板 std::vector::iterator 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20934311/