find_if in set in C++: unresolved overloaded function type

北战南征 提交于 2019-12-25 18:26:20

问题


I have used find_if before with lists and vectors in C++ and it has worked fine. But now, when I try to use it with sets I get the following error:

 error: no matching function for call to ‘find_if(std::set<A, Acmp>::iterator, 
 std::set<A, Acmp>::iterator, <unresolved overloaded function type>)’|

My class is the following:

bool searchForA(A i) {
   return (i.getVal() > 0);
 }


void B::findA()
 {
   set<A, Acmp> cont;
   set<A, Acmp>::iterator it;
   A *a1 = new A(5);
   A *a2 = new A(7);
   cont.insert(*a1);
   cont.insert(*a2);

   it = find_if (cont.begin(), cont.end(), search)
}

Can anyone help me understand where is the problem?


回答1:


There's already a function in the STL called search. That's probably why the compiler can't resolve the right name without a better hint. You could rename your search function. Or if you don't want to do that, try passing ::search into find_if instead.




回答2:


There is more than one function named search — the name is overloaded. To pass a pointer to search into find_if, you need to specify which one. The most straightforward way is to specify static_cast< bool (*)( A ) >( cont ).



来源:https://stackoverflow.com/questions/15239062/find-if-in-set-in-c-unresolved-overloaded-function-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!