views:

66

answers:

3

I'm trying to use STL, but the following doesn't compile. main.cpp:

#include <set>
#include <algorithm>

using namespace std;

class Odp
{
public:

    set<int> nums;

    bool IsOdd(int i)
    {
        return i % 2 != 0;
    }

    bool fAnyOddNums()
    {
        set<int>::iterator iter = find_if(nums.begin(), nums.end(), &Odp::IsOdd);
        return iter != nums.end();
    }
};

int main()
{
    Odp o;
    o.nums.insert(0);
    o.nums.insert(1);
    o.nums.insert(2);
}

The error is:

error C2064: term does not evaluate to a function taking 1 arguments
1>          c:\program files\microsoft visual studio 10.0\vc\include\algorithm(95) : see reference to function template instantiation '_InIt std::_Find_if<std::_Tree_unchecked_const_iterator<_Mytree>,_Pr>(_InIt,_InIt,_Pr)' being compiled
1>          with
1>          [
1>              _InIt=std::_Tree_unchecked_const_iterator<std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>>,
1>              _Mytree=std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>,
1>              _Pr=bool (__thiscall Odp::* )(int)
1>          ]
1>          main.cpp(20) : see reference to function template instantiation '_InIt std::find_if<std::_Tree_const_iterator<_Mytree>,bool(__thiscall Odp::* )(int)>(_InIt,_InIt,_Pr)' being compiled
1>          with
1>          [
1>              _InIt=std::_Tree_const_iterator<std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>>,
1>              _Mytree=std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>,
1>              _Pr=bool (__thiscall Odp::* )(int)
1>          ]

What am I doing wrong?

+3  A: 

It needs to be declared static:

static bool IsOdd(int i)

Otherwise, you'd be asking find_if to call an instance method without an instance.

Matthew Flaschen
+1  A: 

The problem is you are passing a pointer to member function. To call that function you would also need a pointer to this but the find_if doesn't let you to pass it. A solution is to wrap it using a function object, see Boost Bind (http://www.boost.org/doc/libs/1_43_0/libs/bind/bind.html) and Boost Function (http://www.boost.org/doc/libs/1_37_0/doc/html/function.html).

dark_charlie
+1  A: 

IsOdd does not use the class's internals in any way, so don't make it a member function. Instead, pull it out as a standalone function. Then you can call find_if with &IsOdd.

However, there is a benefit to taking things a step further and defining it as a function object:

#include <functional>

struct IsOdd : public unary_function<int, bool>
{
    bool operator()(int i) const { return i % 2 != 0; }
};

Then calling find_if with IsOdd() will inline the code within the find_if loop instead of dereferencing a function pointer and making a function call.

Jon Reid