std :: function作为方法参数

use*_*466 1 c++ stl

我是编程器的新手,我必须为API创建一个使用函数作为参数的方法.所以,在我的标题中,我已经完成了:

typedef void(*target_function)(std::string client_ip);
void my_method(std::string path_name, std::function<target_function> a_function);
Run Code Online (Sandbox Code Playgroud)

在我的cpp中,我尝试过这种方式:

void my_class:: my_method(std::string path_name, std::function<target_function> )
{
    ///
}
Run Code Online (Sandbox Code Playgroud)

但它不会编译.

我对此非常困惑......你能帮助我吗?

编辑:它炒!谢谢,大家,非常快速的回答!

Dan*_*ang 6

@ Cheers-and-hth-alf是对的.但我更喜欢这个:

typedef std::function<void (std::string)> TTargetFunc;
void my_method(std::string path_name, TTargetFunc a_function);
Run Code Online (Sandbox Code Playgroud)