I have the following classes:
class A
{
public:
virtual void myfunc(unsigned char c, std::string* dest) = 0;
};
class B : public class A
{
public:
virtual void myfunc(unsigned char c, std::string* dest);
};
void someOtherFunc(const std::string& str,A *pointerFunc)
{
std::string tmp;
for_each(str.begin(),
str.end(),
std::bind2nd(std::mem_fun(pointerFunc->myfunc), &tmp));
}
I get the following compilation error:
error: no matching function for call to \u2018mem_fun()\u2019
Do you know why?
You’re looking forstd::mem_fun(&A::myfunc).EDIT: You can’t use
mem_funat all here — no overload ofmem_funallows you to make a two argument member function into a functor. You’re going to have to use something likeboost::bind/std::tr1::bind(If you have TR1)/std::bind(If you have C++0x) or you’re going to have to write your own functor.Note that even if
mem_funwas able to do this sort of binding, thenstd::bind2ndwould fail, becausebind2ndexpects a functor taking two arguments, and binding a member function pointer like this is going to produce a functor with three arguments.You have a few ways around this:
std::for_each.myfuncdoesn’t depend on members of the class to which it belongs (in which case it shouldn’t have ever been put into a class in the first place)