c++ - 使用 Boost.Python 导出非默认可构造的类

标签 c++ boost-python

如何使用 Boost.Python 导出非默认可构造的类?

这段代码:

class EventHandle {
 public:
  EventHandle() = delete;
  EventHandle(boost::shared_ptr<EventManager> const& em): event_manager_(em) {}
  EventHandle(EventHandle const&) = delete;
  ~EventHandle();
  shared_ptr<EventManager> event_manager_;
}
class_<EventHandle, noncopyable,
  init<boost::shared_ptr<EventManager> const&>>("EventHandle")

出现以下错误:

/opt/local/include/boost/python/pointee.hpp: In instantiation of 'boost::python::detail::pointee_impl<false>::apply<boost::python::init<const boost::shared_ptr<EventManager>&> >':
/opt/local/include/boost/python/pointee.hpp:38:1:   instantiated from 'boost::python::pointee<boost::python::init<const boost::shared_ptr<EventManager>&> >'
/opt/local/include/boost/mpl/eval_if.hpp:38:31:   instantiated from 'boost::mpl::eval_if<boost::is_convertible<boost::python::init<const boost::shared_ptr<EventManager>&>*, EventHandle*>, boost::mpl::identity<boost::python::init<const boost::shared_ptr<EventManager>&> >, boost::python::pointee<boost::python::init<const boost::shared_ptr<EventManager>&> > >'
/opt/local/include/boost/python/object/class_metadata.hpp:179:13:   instantiated from 'boost::python::objects::class_metadata<EventHandle, boost::noncopyable_::noncopyable, boost::python::init<const boost::shared_ptr<EventManager>&>, boost::python::detail::not_specified>'
/opt/local/include/boost/python/class.hpp:174:42:   instantiated from 'boost::python::class_<EventHandle, boost::noncopyable_::noncopyable, boost::python::init<const boost::shared_ptr<EventManager>&> >::id_vector'
/opt/local/include/boost/python/class.hpp:627:55:   instantiated from 'boost::python::class_<T, X1, X2, X3>::class_(const char*, const char*) [with W = EventHandle, X1 = boost::noncopyable_::noncopyable, X2 = boost::python::init<const boost::shared_ptr<EventManager>&>, X3 = boost::python::detail::not_specified]'
/Users/neil/nn/src/core/python_event.cc:21:66:   instantiated from here
/opt/local/include/boost/python/pointee.hpp:28:44: error: no type named 'element_type' in 'class boost::python::init<const boost::shared_ptr<EventManager>&>'
make[3]: *** [CMakeFiles/distributions.dir/core/python_event.cc.o] Error 1
make[2]: *** [CMakeFiles/distributions.dir/all] Error 2
make[1]: *** [CMakeFiles/distributions.dir/rule] Error 2
make: *** [distributions] Error 2

最佳答案

您需要使用 init<...> 公开构造函数.例如,对于采用两个整数的构造函数:

class_<MyClass>("MyClass", init<int, int>())
    ....

请注意,您需要将 initclass_里面参数而不是单独的 .def()调用,否则 Boost 将假设您有一个默认构造函数。

参见 the tutorial section about constructors .

编辑: 对于您的代码,请尝试:

class_<EventHandle, noncopyable>("EventHandle", 
    init<boost::shared_ptr<EventManager> const&>())

关于c++ - 使用 Boost.Python 导出非默认可构造的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5880223/

相关文章:

c++ - self 强加的 try/catch 不需要的整数

c++ - 在程序运行时编译程序

c++ - RegisterRawInputDevices函数使用

python - 如何在 boost::python 插件中设置 sys.argv

c++ - boost.python return_internal_reference 策略

c++ - boost::python 中静态属性的文档字符串

c++ - LLU 号码后缀错误

c++ - 牛顿法求立方根,结果每次都是0

c++ - 解释 Python 扩展多线程

c++ - 嵌入C++的boost python中的正则表达式