如何声明一个迭代器
std::map <T, Point <T> *> ,
哪里:
template <typename T>
struct TList
{
typedef std::vector < std::map <T, Point <T> *> > Type;
};
在下面的代码中
int main ()
{
....
std::map <T, Point <T> *> ::iterator i_map; //Error
...
}
g++ 显示此错误:
error: dependent-name ` std::map<T,Point<T>*,std::less<_Key>,std::allocator<std::pair<const T, Point<T>*> > >::iterator' is parsed as a non-type, but instantiation yields a type
note: say `typename std::map<T,Point<T>*,std::less<_Key>,std::allocator<std::pair<const T, Point<T>*> > >::iterator' if a type is meant
最佳答案
使用 typename
作为:
typename std::map<T, Point <T> *>::iterator i_map;
//^^^^^^^^ here!
因为 iterator
是从属名称(因为它取决于 map 的类型 参数 T
),所以 typename
这里是必需的。
阅读此常见问题解答以获取详细说明:
Where and why do I have to put the "template" and "typename" keywords?
关于C++,std::map 的迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6021686/