使用Python类中的C++类继承方法(SWIG)

2024-10-04 01:28:08 发布

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

<>我现在使用SWIG使我的主要C++程序中的小模块更容易实现。类架构如下:

在福奥水电站以下内容:

class Foo
{
public:
  virtual int pureFunc() = 0;
  int func() { return (42); }
};

文件.i:

^{pr2}$

在文件.py以下内容:

import sys
sys.path.append('.')
import foo

class Bar(foo.Foo):
    def __init__(self):
        pass
    def pureFunc(self):
        return 21

lol = Bar()
print lol.pureFunc()
print lol.func()

然后,我使用以下命令生成swig包装器:

swig -python -c++ file.i

然后编译。这样:

g++ -fPIC -shared -o _foo.so file_wrap.cxx -I/usr/include/python2.7 -lpython2.7

当我试图运行python脚本时,我得到了以下错误:

# python file.py 
21
Traceback (most recent call last):
  File "file.py", line 13, in <module>
    print lol.func()
  File "/home/volent/dev/CPP/cython/foo.py", line 84, in func
    def func(self): return _foo.Foo_func(self)
TypeError: in method 'Foo_func', argument 1 of type 'Foo *'
# _

这表明纯方法的使用是有效的,但是我不能使用.hpp文件中已经定义的方法。在

我试过阅读SWIG文档,我看到的关于抽象类和继承的东西只有34.4.7 C++ Classes和{a2}。我看不出有什么能提到这样的案子。在


Tags: 文件inpyselfreturnfoodefclass