[英]Using derived class (C++) by Python BOOST
我有要在Python中使用的C ++类:
#include "ExtraClass.h"
class CApp
{
...
ExtraClass Bar; //there is function Foo()
}
BOOST_PYTHON_MODULE( CApp )
{
class_<ExtraClass>("ExtraClass",init<>())
.def("Foo",&ExtraClass::Foo)
;
class_<CApp>("CApp", init<>())
.def_readonly("Bar", &CApp::Bar)
;
编译正常。 所以我有CApp.so文件要导入到Python中。 因此问题开始于Python:
from CApp import *
class pyApp(CApp):
def __init__(self):
print "<--INIT-->"
CApp = CApp()
pyApp = pyApp()
print CApp.Bar.Foo()
print pyApp.Bar.Foo()
输出:
<--INIT-->
FOO // <- this is from CApp.Bar.Foo()
Traceback (most recent call last):
File "./pytest.py", line 16, in <module>
print pyApp.Bar.Foo()
Boost.Python.ArgumentError:
Python argument types in None.None(pyApp)
did not match C++ signature: None(CApp {lvalue})
如果在派生类中实现init (),则需要确保调用基本init (),否则它将没有基本成员。
from CApp import *
class pyApp(CApp):
def __init__(self):
CApp.__init__(self)
print "<--INIT-->"
CApp = CApp()
pyApp = pyApp()
print CApp.Bar.Foo()
print pyApp.Bar.Foo()
我遇到了同样的问题,并在这里找到了解决方案-https: //stackoverflow.com/a/6396839 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.