从使用boost::python生成的python类继承

2024-09-30 16:35:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我在继承使用boost::python生成的python类时遇到了一个问题。在

我有一个类,其函数Print()的定义如下:

void CMagnet::Print()
{
  cout << "Hello" << endl;
}

接口在我的.cpp文件中定义如下:

^{pr2}$

原则上,该模块可以工作,但我遇到了继承方面的问题。下面是一个例子:

from CMagnet import CMagnet

class DerMagnet(CMagnet):
    def __init__(self):
        self.Print()

a = CMagnet()
a.Print()

b = DerMagnet()

我得到的是:

hirbel>;python der_测试.py在

Hello

Traceback (most recent call last):
  File "der_test.py", line 10, in <module>
    b = DerMagnet()
  File "der_test.py", line 5, in __init__
    self.Print()
Boost.Python.ArgumentError: Python argument types in
    CMagnet.Print(DerMagnet)
did not match C++ signature:
    Print(CMagnet {lvalue})

这意味着当我实例化CMagnet类时,可以毫无问题地调用Print()方法,但是当我从它继承并且派生类尝试调用该方法时,self参数会自动作为第一个参数插入,签名是错误的。我该如何解决这个问题?在

非常感谢你的帮助。在


Tags: 方法inpytestselfhello参数定义