2011-05-04 2 views
2

Boost.Python을 사용하여 기본 구성이 아닌 클래스를 내보내려면 어떻게해야합니까?Boost.Python을 사용하여 기본 구성이 아닌 클래스 내보내기

이 코드 : 당신은 init<...>와 생성자를 노출 할 필요가

/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

:

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") 

다음과 같은 오류를 제공합니다.

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

주 당신이 기본 생성자가 가정합니다 사용자가 별도의 .def() 전화에 비해 class_ 매개 변수 내에서 init을 배치, 또는 부스트해야합니다 예를 들어, 생성자에 대한 두 가지의 int를 복용.

the tutorial section about constructors을 참조하십시오.

편집 : 코드를 들어보십시오

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

감사합니다. 나는 이것을 시도했다. 나는 그 질문을 갱신 할 것이다. –

+0

@Neil :'.def'가 아닌'class_' 매개 변수에'init'을 넣으십시오. 그렇지 않으면 Boost는 기본 생성자를 가지고 있다고 가정합니다. – interjay

+0

고마워요! 아직도 작동하지 않습니다 (업데이트 된 질문 참조). –

1

당신이 no_ini톤를 사용하여 시도 적이 있습니까? 문서는 이것이 생성자를 원하지 않을 때를위한 것이라고 말하지만, 명시 적으로 요구하는 것과 결합 할 수 있습니다.

class_<EventHandle, noncopyable, 
    init<boost::shared_ptr<EventManager> const&> > 
    ("EventHandle", no_init) 
관련 문제