其他分享
首页 > 其他分享> > C,std :: map的迭代器

C,std :: map的迭代器

作者:互联网

如何声明迭代器

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!

因为迭代器是一个依赖名称(因为它取决于map的类型参数T),所以这里需要typename.

阅读此FAQ以获取详细说明:

Where and why do I have to put the “template” and “typename” keywords?

标签:c,iterator,stdmap
来源: https://codeday.me/bug/20190903/1797247.html