2
我有以下代码。由编译器无法识别的模板函数中的映射迭代器
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给我下面的错误。
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
不知何故“它”没有被初始化,Sam Hain在这里发生了什么?!
重复包括:http://stackoverflow.com/questions/1301380/g-is-not-a-type-error HTTP://计算器。 com/questions/2841757/c-template-is-not-derived-from-type http://stackoverflow.com/questions/2931345/c-trouble-with-dependent-types-in-templates – 2010-07-06 08:47:09
旁注:你确定你想通过值而不是const引用传递你的地图,并使用const_iterator? – pmr 2010-07-06 09:46:32
pmr,感谢您的建议。常量使用是我还没有进入的习惯,需要养成周围的习惯。 – Eli 2010-07-07 01:00:34