码迷,mamicode.com
首页 > 其他好文 > 详细

使用STL map和模板时遇到的一个错误

时间:2015-03-12 00:59:22      阅读:397      评论:0      收藏:0      [点我收藏+]

标签:

代码:

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

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

使用STL map和模板时遇到的一个错误

标签:

原文地址:http://www.cnblogs.com/shenwudi/p/4331357.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!