我有一个参数类型为 的函数function<void(int)>。
我想要一个默认参数function<void(int)>。
这是示例代码:
class worker {
public:
void do_the_job(function<void(int)> call_back = &worker::_log_call_back) {
// doing the job ...
call_back(1);
// doing the job ...
call_back(-1);
}
private:
void _log_call_back(int status){
cout << "status: " << status << endl;
}
};
Run Code Online (Sandbox Code Playgroud)
当我编译上面的代码时,出现以下错误:
worker().do_the_job(); // visual studio 2013 error
//error C2664: 'void std::_Func_class<_Ret,int>::_Set(std::_Func_base<_Ret,int> *)' :
//cannot convert argument 1 from '_Myimpl *' to 'std::_Func_base<_Ret,int> *' c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional
Run Code Online (Sandbox Code Playgroud)
为什么 ?以及如何解决它?
_log_call_back有 aworker *作为隐藏参数,而function<void(int)>没有,因此它们不兼容。您可以使用static void _log_callback来删除隐藏参数。或者,您可以使用指向成员函数的指针,例如void (worker::*p)(int) = worker::_log_call_back.
| 归档时间: |
|
| 查看次数: |
377 次 |
| 最近记录: |