2011-05-04 68 views
2

如何使用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 

回答

4

您需要與init<...>暴露的構造。例如,對於一個構造函數取兩個整數:

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

請注意,您需要把class_參數內init,而不是在一個單獨的.def()呼叫,或加速會假設你有一個默認的構造函數。

請參閱the tutorial section about constructors

編輯: 爲您的代碼,請嘗試:

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

謝謝。我雖然嘗試過。我會更新這個問題。 – 2011-05-04 07:54:14

+0

@Neil:將'init'放在'class_'參數中,就像我的例子中,而不是單獨的'.def'。否則Boost會認爲你有一個默認的構造函數。 – interjay 2011-05-04 07:56:59

+0

非常感謝!仍然不起作用(查看更新後的問題),但距離更近。 – 2011-05-04 08:06:05

1

您是否嘗試過使用no_ini噸?文檔說這是爲了當你不需要構造函數時,但也許你可以將它與明確要求的結合起來?

class_<EventHandle, noncopyable, 
    init<boost::shared_ptr<EventManager> const&> > 
    ("EventHandle", no_init) 
相關問題