map迭代器在模板函数中无法被编译器识别 [英] map iterator in template function unrecognized by compiler
本文介绍了map迭代器在模板函数中无法被编译器识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码。
template<class key,class val>
bool has_key(key chkey,std::map<key,val> map){
for (std::map<key,val>::iterator it = map.begin(); #line 13 referenced by gcc
it!=map.end(); ++it){
if(chkey == it->first) return true;
}
return false;
}
GCC给我以下错误。
GCC is giving me the following error.
objects.hpp: In function `bool has_key(key, std::map<key, val, std::less<_Key>,
std::allocator<std::pair<const _Key, _Tp> > >)':
objects.hpp:13: error: expected `;' before "it"
objects.hpp:14: error: `it' was not declared in this scope
推荐答案
你需要 typename
关键字:
for (typename std::map<key,val>::iterator it = map.begin(); #line 13 referenced by gcc
it!=map.end(); ++it){
$ b b
另请参阅: http://stackoverflow.com/questions/1123080/why -do-we-need-typename-这里
这是因为你在一个模板定义和迭代器
是一个依赖名称。
This is because you are in a template definition and iterator
is a dependent name. This has been asked before.
http://stackoverflow.com/questions/1301380/g-is-not-a-type-error
http://stackoverflow.com/questions/2841757/c-template-is-not-derived-from-type
http://stackoverflow.com/questions/2931345/c-trouble-with-dependent-types-in-templates
这篇关于map迭代器在模板函数中无法被编译器识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文