2011-08-03 89 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> >