在福奥水电站以下内容:
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}。我看不出有什么能提到这样的案子。在
您忘记了调用
Bar
的父类的__init__
方法。将您的__init__
方法替换为:这应该让您的
Bar
类知道Foo
方法。在相关问题 更多 >
编程相关推荐