-1

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?

hivert
  • 10,579
  • 3
  • 31
  • 56
FranXh
  • 4,481
  • 20
  • 59
  • 78
  • No I have not included that inside class A for the set. IF I may ask, why would overloading < affect find_if? – FranXh Mar 06 '13 at 04:42
  • How about declaring `search (A const &i)` – Mark Taylor Mar 06 '13 at 04:44
  • That's a lovely way to leak memory. The pointers are completely unnecessary. By the way, [this](http://liveworkspace.org/code/3v0m5c%247) is close and works. – chris Mar 06 '13 at 04:45
  • 1
    set element needs to be weak ordered, try to add `operator<` your code should compile, also as chris mentions, your code leaks memory – billz Mar 06 '13 at 04:46
  • Is the definition of the `search()` function you gave above exactly the same as in your code? And is it the only definition for that function, or are there any overloads? – jogojapan Mar 06 '13 at 04:46
  • Ok, so I changed the name of the function, and I added to the set a Comparator. I will take care of the pointers too. – FranXh Mar 06 '13 at 04:49
  • Sorry I take that back. Apparently the name of the function was the problem. – FranXh Mar 06 '13 at 04:50
  • @billz The OP apparently uses a customised comparator `Acmp` for the set. So, adding a `operator<` won't change anything. Also, the comparison operator isn't relevant when `find_if` is used. – jogojapan Mar 06 '13 at 04:55
  • @jogojapan yeah, looks like it. I think Dave's answer is on the right direction. – billz Mar 06 '13 at 04:58

2 Answers2

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.

Dave Johnson
  • 411
  • 3
  • 9
  • That's what I thought initially too. I tried to change the name of the function. Still the same error. – FranXh Mar 06 '13 at 04:42
  • No, changing the name of the function does fix that error. You're getting another big error because A needs a comparison operator, like the other people said. – Dave Johnson Mar 06 '13 at 04:50
  • Yes changing the name fixed the problem. – FranXh Mar 06 '13 at 04:51
1

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

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421