首页 新闻 会员 周边

C++函数模板

0
悬赏园豆:5 [已解决问题] 解决于 2015-07-19 18:58

一个简单的查找函数如下:

template<typename Iterator, typename Object>
Iterator find(Iterator start, Iterator end, const Object &x)
{
    Iterator iter = start;
    while (iter != end&&*iter != x)
        iter++;
    return iter;
}

然后进行调用用下面的语句出错:

list<int> lst = { 1, 4, 5, 6 };
list <int>::iterator itr=find(lst.begin(),lst.end(),10);

错误如下:

错误 1 error C2668: “find”: 对重载函数的调用不明确 e:\developer\vsworkspace\algorithm\algorithm_cplusplus\hw_3\h1to5.cpp 45 1 hw_3

(PS:没有定义两个find---以上应该表示调用模板有歧义?)

----------------------------------

虽然错误给的很明确,但还是比较困惑该怎么改~

希望解答,谢谢

C++
Nooooooo的主页 Nooooooo | 初学一级 | 园豆:192
提问于:2015-07-19 11:43
< >
分享
最佳答案
1

和std库冲突,VS2013下 _InIt std::find<_InIt, _Ty>(_InIt _First, _InIt _Last, const _Ty & _Val),已经有这个定义了

解决方法2个:1 换成其他名字;2 将find放到自己的名字空间中

收获园豆:5
2012 | 高人七级 |园豆:21230 | 2015-07-19 16:37

thx~

晕倒=。=   想到这个问题,但记得用了库名错误信息不是这个。~

感谢感谢。

Nooooooo | 园豆:192 (初学一级) | 2015-07-19 18:58
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册