博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用STL map和模板时遇到的一个错误
阅读量:5089 次
发布时间:2019-06-13

本文共 1460 字,大约阅读时间需要 4 分钟。

代码:

template<typename T, typename U>

bool MapContainsKey(const map<T,U>& m,T key)
{
for (map<T, U>::iterator it = m.begin(); it != m.end(); ++it)//差点玩死我
{
if (it->first == key)
{
return true;
}
}
return false;
}

报错如下:=======================================================================

1>e:\program files\project\myregex20150310\test\source.cpp(38):

error C2440: 'initializing' : cannot convert from
'std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>'
to
'std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>'
1> with
1> [
1> _Kty=std::string
1> , _Ty=std::vector<int,std::allocator<int>>
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
1> e:\program files\project\myregex20150310\test\source.cpp(70) : see reference to function template instantiation
'bool MapContainsKey<std::string,std::vector<int,std::allocator<_Ty>>>
(const std::map<std::string,std::vector<_Ty,std::allocator<_Ty>>,
std::less<_Kty>,
std::allocator<std::pair<const _Kty,std::vector<_Ty,std::allocator<_Ty>>>>> &,
T)'
being compiled
1> with
1> [
1> _Ty=int
1> , _Kty=std::string
1> , T=std::string
1> ]

===================================================================================

解决方案当然是把iterator改成const_iterator

===================================================================================

转载于:https://www.cnblogs.com/shenwudi/p/4331357.html

你可能感兴趣的文章