如何创建模板类型迭代器的STL对象? 我正在尝试创建一个模板类型的迭代器的 STL 对象(比如 Vector),如下所示
vector<vector<T>::iterator> it1;
这在 g++ 和 VC++ 中都失败了。两个编译器几乎说同样的话
g++
Main.cpp:8:49: error: type/value mismatch at argument 1 in template parameter li
st for `template<class _T1, class _T2> struct std::pair'
Main.cpp:8:49: error: expected a type, got `std::vector<T>::iterator'
VC++
error C2923: 'std::vector' : 'std::vector<T>::iterator' is not a valid template type argument for parameter '_Ty'
例如,如果你正在创建一个具体类型的迭代器,它就可以正常工作
vector<vector<int>::iterator> it1;
这是一个最小的失败例子
#include<vector>
#include<iterator>
using namespace std;
template<typename T>
class Spam {
public:
vector<vector<int>::iterator> it1; #Compiles Fine
vector<vector<T>::iterator> it2; #Fails
};
注意
我从答案中了解到,如果类型依赖,则需要在关键字 typename
前加上前缀。如果是这样的话
vector< vector<T> > it;
也应该会失败,但事实并非如此。它似乎只是失败并且需要一个 typename
关键字当且仅当从属名称是一个 typedef
最佳答案
关于c++ - 如何创建模板类型迭代器的 STL 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10397338/