Boost Python:无法从Python扩展类方法中调用基方法
我的问题如下: 我试图用PosithPython将第三方C++库绑定到Python。 这个库有一个类元素和一个类所有者(后者拥有元素对象的所有权) 为了使事情变得有趣,元素定义了一个纯虚拟方法“get_float”。 元素应该在python中进行子类化。以下是该库的简化版本:Boost Python:无法从Python扩展类方法中调用基方法,python,binding,boost-python,Python,Binding,Boost Python,我的问题如下: 我试图用PosithPython将第三方C++库绑定到Python。 这个库有一个类元素和一个类所有者(后者拥有元素对象的所有权) 为了使事情变得有趣,元素定义了一个纯虚拟方法“get_float”。 元素应该在python中进行子类化。以下是该库的简化版本: // m1.hpp ----------------------------------------------------------------- class Element { public: int ge
// m1.hpp -----------------------------------------------------------------
class Element {
public:
int get_int() { return -1; }
virtual float get_float() = 0;
};
class Owner {
Element *m_element;
public:
Owner(): m_element(NULL) {}
~Owner() { delete m_element; }
void set_element(Element* e) {
delete m_element;
m_element = e;
};
Element* get_element() { return m_element; }
};
下面是我如何绑定到python的:
// m1_py.cpp ----------------------------------------------------------------
class ElementWrap : public Element,
public wrapper<Element>
{
public:
PyObject* self;
ElementWrap(PyObject* self_): Element(), self(self_)
{ Py_INCREF(self); }
~ElementWrap() { Py_DECREF(self); }
virtual float get_float() { return call_method<float>(self, "get_float"); }
};
void set_element(Owner& owner, auto_ptr<Element> e) {
owner.set_element(e.get());
e.release();
}
BOOST_PYTHON_MODULE(m1) {
class_<Element, auto_ptr<ElementWrap>, boost::noncopyable>("Element", init<>())
.def("get_int", &Element::get_int)
.def("get_float", &Element::get_float)
;
register_ptr_to_python<auto_ptr<Element> >();
implicitly_convertible<auto_ptr<ElementWrap>, auto_ptr<Element> >();
class_<Owner>("Owner", init<>())
.def("set_element", &set_element)
.def("get_element", &Owner::get_element, return_internal_reference<>())
;
}
我在最后一行得到一个异常:
Traceback (most recent call last):
File "m2.py", line 16, in <module>
print (element.get_float())
File "m2.py", line 8, in get_float
return 1.0 + self.get_int()
Boost.Python.ArgumentError: Python argument types in
Element.get_int(SubElement)
did not match C++ signature:
get_int(Element {lvalue})
回溯(最近一次呼叫最后一次):
文件“m2.py”,第16行,在
打印(element.get\u float())
文件“m2.py”,第8行,在get_float中
返回1.0+self.get_int()
ArgumentError:中的Python参数类型
元素。获取_int(子元素)
与C++签名不匹配:
get_int(元素{lvalue})
似乎当我将所有权传递给python时,我就失去了调用
来自基类的方法
有人能帮我吗
提前谢谢
蒂亚戈
Traceback (most recent call last):
File "m2.py", line 16, in <module>
print (element.get_float())
File "m2.py", line 8, in get_float
return 1.0 + self.get_int()
Boost.Python.ArgumentError: Python argument types in
Element.get_int(SubElement)
did not match C++ signature:
get_int(Element {lvalue})