2011-08-03 39 views
0

我得到一些编译时错误,我不明白这是为什么。下面的代码将拒绝编译,让我有以下错误:Boost.Bind与功能和Python

错误C2664: '无效(的PyObject *,为const char *,提振::类型*)':无法从“常量字符转换参数1 * 'to'PyObject *'
error C2664:'void(PyObject *,const char *,boost :: type *)':无法将参数3从'boost :: shared_ptr'转换为'boost :: type *'

PyObject* self = ...; 
const char* fname = "..."; 
boost::function<void (boost::shared_ptr<Event>)> func; 
func = boost::bind(boost::python::call_method<void>, self, fname, _1); 

回答

1
boost::python::call_method

由几个重载函数使用不同数目的参数,这样定义:

template <class R> 
R call_method(PyObject* self, char const* method); 
template <class R, class A1> 
R call_method(PyObject* self, char const* method, A1 const&); 
template <class R, class A1, class A2> 
R call_method(PyObject* self, char const* method, A1 const&, A2 const&); 
... 

当你直接调用它(例如call_method<void>(self, name, arg1, arg2)),编译器可以自动选择正确的重载和模板化参数类型。但是,当你传递一个函数指针call_methodbind,您需要手动指定的过载和参数类型,使用:

call_method<ReturnType, Arg1Type, Arg2Type, ...> 

或在这种情况下:

call_method<void, boost::shared_ptr<Event> >