find_if in set in C++: unresolved overloaded funct

2019-09-19 15:35发布

问题:

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 ).



标签: c++ set stdset