Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ boostpython-Unbound方法调用_C++_Python 2.7_Boost Python - Fatal编程技术网

C++ boostpython-Unbound方法调用

C++ boostpython-Unbound方法调用,c++,python-2.7,boost-python,C++,Python 2.7,Boost Python,我尝试使用Python嵌入到C++中:Boosi::Python。p> 我的嵌入式脚本应该使用decorator来注册它们的方法,如下所示: class Test: def __init__(self, object_id): self.object_id = object_id @decorator('my_id_1') def func1(self): print("{}.func1".format(self.object

我尝试使用Python嵌入到C++中:Boosi::Python。p> 我的嵌入式脚本应该使用decorator来注册它们的方法,如下所示:

class Test:

    def __init__(self, object_id):
        self.object_id = object_id


    @decorator('my_id_1')
    def func1(self):    
        print("{}.func1".format(self.object_id)) 

<代码>装饰器< /C> >在C++侧声明,定义方法<代码>在方法调用之前,一切都按预期工作,从而导致SIGSEGV或SIGARBT

下面是我想在Python中执行的一个示例:

#CPP side
item = {}

class decorator:
    def __init__(self, _id):
        self._id = _id

    def __call__(self, func):
        item[self._id] = func #saved as PyObject* in CPP
        print func
        return func

#Script side            
class Test(CppBase):

    def __init__(self, object_id):
        CppBase.__init__(self)
        self.object_id = object_id


    @decorator('my_id_1')
    def func1(self):    
        print("{}.func1".format(self.object_id)) 


    @decorator('my_id_2')
    def func2(self):    
        print("{}.func2".format(self.object_id)) 

#CPP side
obj1 = Test("obj1")

item['my_id_1'](obj1) #The crash append here
要进行调用,我将使用以下函数:
boost::python::call(my_PyObject\u func,boost::ref(my_obj\u实例))

<>我不会把我所有的C++代码都放进去,因为我实际上正在更新一个旧Python C API所做的工作项目,整个API相当庞大。但是,如果你认为我忘记了其中重要的部分,请告诉我,我会发布这些部分。此外,我删除了许多简单的检查,例如确保全局Python变量包含我的对象、没有发生Python错误或散列包含请求的id,以使代码更轻松

这是我的C++对象定义< /p>

class CppBase: public boost::python::object {
    public:
        CppBase();

        void open(std::string, boost::python::tuple arg);

        void setRoutes(const std::hash<std::string, const Route*>&);

        inline std::hash<std::string, const Route*>*const routes() const { return route; }

    private:
      std::string name;
      QHash<std::string, const Decorator*> *route;
};

class Decorator {
    public:
        Decorator(std::string identifier);

        PyObject* call(PyObject* func);

        void invoke(boost::python::object&, boost::python::tuple) const;

        static void commit(CppBase&);
    private:
        PyObject* method;

        std::string identifier;

        static std::hash<std::string, const Decorator*> routes;
};
下面是Python脚本示例,使用以下示例运行:

class MyScriptSubClass(CppApp):

    def __init__(self, object_id):
        CppBase.__init__(self)
        self.object_id = object_id


    @decorator('my_id_1')
    def func1(self):    
        print("{}.func1".format(self.object_id)) 
以下是我如何努力让一切顺利进行的

//... Creating Python context, Executing the Script file...

boost::python::object cls(main_module.attr("MyScriptSubClass")); //Getting the classDefinition

CppBase core = boost::python::extract<CppBase>(cls()); //Instanciating the object with the previous catched definition

Decorator::commit(core); //Save all the decorator intercepted until now into the object

core.open('my_id_1'); //Calling the function matching with this id
/。。。正在创建Python上下文,正在执行脚本文件。。。
boost::python::object cls(main_module.attr(“MyScriptSubClass”)//获取类定义
CppBase core=boost::python::extract(cls())//使用先前捕获的定义实例化对象
Decorator::commit(核心)//将迄今为止截获的所有decorator保存到对象中
core.open('my_id_1')//调用与此id匹配的函数
我希望我把一切都说清楚了

提前,谢谢

class MyScriptSubClass(CppApp):

    def __init__(self, object_id):
        CppBase.__init__(self)
        self.object_id = object_id


    @decorator('my_id_1')
    def func1(self):    
        print("{}.func1".format(self.object_id)) 
//... Creating Python context, Executing the Script file...

boost::python::object cls(main_module.attr("MyScriptSubClass")); //Getting the classDefinition

CppBase core = boost::python::extract<CppBase>(cls()); //Instanciating the object with the previous catched definition

Decorator::commit(core); //Save all the decorator intercepted until now into the object

core.open('my_id_1'); //Calling the function matching with this id