I am trying to learn std::function
and here's my code:
#include <iostream>
#include <functional>
struct Foo {
void print_add(int i){
std::cout << i << '
';
}
};
typedef std::function<void(int)> fp;
void test(fp my_func)
{
my_func(5);
}
int main(){
Foo foo;
test(foo.print_add);
return 0;
}
Compiler Error:
error: cannot convert 'Foo::print_add' from type 'void (Foo::)(int)' to type 'fp {aka std::function<void(int)>}'
test(foo.print_add);
How can I make this work, i.e how can I pass a member function as a parameter?
See Question&Answers more detail:os