簡體   English   中英

通過Python BOOST使用派生類(C ++)

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM