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?
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
intofind_if
instead.There is more than one function named
search
— the name is overloaded. To pass a pointer tosearch
intofind_if
, you need to specify which one. The most straightforward way is to specifystatic_cast< bool (*)( A ) >( cont )
.