0

我得到一些编译时错误,我不明白为什么会这样。以下代码将拒绝编译,给我以下错误:

错误 C2664: 'void (PyObject *,const char *,boost::type *)' : 无法将参数 1 从 'const char *' 转换为 'PyObject *'
错误 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);
4

1 回答 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_methodintobind时,您需要手动指定重载和参数类型,方法是:

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

或者在这种情况下:

call_method<void, boost::shared_ptr<Event> >
于 2011-08-03T14:34:57.817 回答