class A {
public:
std::function<void(int)> f_;
void print_num(int i) {
cout << i;
}
void setFuntion(std::function<void(int)> f) {
f_=f;
}
void run() {
setFunction(print_num);
}
};
this doesn’t work. i get note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::function<void(int)>’ and other errors.
If I put the definition of print_num outside of the class. everything works. i tried adding &A::, A:: and this. nothing helped.
print_numis a non-static member function, which means that it has an implicit first argument of typeA*. You can, for instance, pass that by using a lambda:or use
bind, see herec++ Trouble casting overloaded function: <unresolved overloaded function type>