0

私はブーストのためのC++クラスとそのラッパーを持っています::python:

class CApp
{
 public:
   virtual bool FOOs (){}; //does not matter for now
   bool Run( const char * First,const char * Last) 
   {
     ...
     return "Running..."
   };

struct pyApp : CApp, wrapper<CApp>  //derived class 
{
   ... // wrappers for virtual methods
}

#include <boost/python.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE( myApp )
{
class_<pyApp, boost::noncopyable>("CApp", init<>()   )
            ...
    .def("Run",&pyMOOSApp::Run);
}

コンパイルはOKです。しかし、Python コードを呼び出しているとき

from myApp import *

class pyApp(CApp):
def __init__(self):
    print "INIT-->"

CClass = CApp()
pyClass = pyApp()
CClass.Run('myApp','bar')
pyClass.Run('myApp','bar')

エラーがあります:

INIT-->
Running... // this is from CClass.Run
Traceback (most recent call last):
File "./pytest.py", line 18, in <module>
pyClass.Run('myApp','bar')
Boost.Python.ArgumentError: Python argument types in
CApp.Run(pyApp, str, str)
did not match C++ signature:
Run(CApp {lvalue}, char const*, char const*)

そこで、派生クラスの C++ コードに配置された str を char に変換する Run メソッドのラッパーを作成しようとしました。

bool Run(std::string a, std::string b) {
    char * cstrA;
    char * cstrB;
    cstrA = new char[a.size()+1];
    cstrB = new char[b.size()+1];
    strcpy(cstrA,a.c_str());
    strcpy(cstrB,b.c_str());
    return this -> CApp::Run(cstrA, cstrB);
}

しかし、唯一の変更は最後のストロークにありました。

did not match C++ signature:
Run(pyApp {lvalue}, std::string, std::string)

Run メソッドのラッパーが悪いと確信しているので、助けていただければ幸いです。ありがとうございました。

4

1 に答える 1

0

あなたの他の質問 ( Python BOOST による派生クラス (C++) の使用 ) を見たところ、これには同じ解決策があると思います。派生クラスで init を提供する場合は、基本クラスで init() を呼び出す必要があります。ここで私のソリューションを参照してください - https://stackoverflow.com/a/14742937/82896

于 2013-02-07T07:46:16.057 に答える