我有我想在 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})