As @ steveo225 noted, f in this context is of type void (a::*)(int) , not void (*)(int) . There are two approaches to fixing this: the first is to make b::foreach_x_do a member function template that accepts any type being called:
class b { int* x; public: b() : x() { } template<typename F> void foreach_x_do(F f) { while(*x++) f(*x); } }; class a { b b_inst; public: void f(int x) { } a() : b_inst() { b_inst.foreach_x_do(std::bind(&a::f, this, _1)); } };
The second is to save the b::foreach_x_do non-template and use std::function<> instead of the function pointer:
class b { int* x; public: b() : x() { } void foreach_x_do(std::function<void(int)> const& f) { while(*x++) f(*x); } }; class a { b b_inst; public: void f(int x) { } a() : b_inst() { b_inst.foreach_x_do(std::bind(&a::f, this, _1)); } };
In any case, replace std::bind and std::function with your counterparts boost:: if your compiler is too old to supply std:: or std::tr1:: implementations. Also note that if you have a C ++ 11 compiler, you can use lambda instead of bind .
source share