这与this密切相关问题
无论这是否是编码实践,我都遇到过如下所示的代码
测试.hh
#include <vector>
using std::vector;
class Test
{
public:
vector<double> data;
};
我正在尝试使用以下接口(interface)文件使用 swig3.0 来 swig
测试.i
%module test_swig
%include "std_vector.i"
namespace std {
%template(VectorDouble) vector<double>;
};
%{
#include "test.hh"
%}
%naturalvar Test::data;
%include "test.hh"
以及以下测试代码
测试.py
t = test.Test()
jprint(t)
a = [1, 2, 3]
t.data = a # fails
这样做会出现以下错误
in method 'Test_data_set', argument 2 of type 'vector< double >'
可以通过更改 using std::vector
来解决此问题在 test.hh 中到 using namespace std
或者删除 using std::vector
和改变vector<double>
至std::vector<double>
。这不是我想要的。
问题是我按原样得到了这段代码。我不允许进行更改,但我应该仍然通过 SWIG 使 python 中的所有内容可用。这里发生了什么?
提前致谢。
最佳答案
对我来说,这看起来 SWIG 不支持 using std::vector;
陈述正确。我认为这是一个 SWIG 错误。我可以想到以下解决方法:
- 添加
using namespace std;
到 SWIG 接口(interface)文件(这只会影响包装器的创建方式;using
语句不会输入 C++ 代码) - 添加
#define vector std::vector
到 SWIG 接口(interface)文件(仅当vector
从未用作std::vector
时才有效) - 将声明从头文件复制到 SWIG 接口(interface)文件,并更改
vector
至std::vector
。这将导致 SWIG 生成正确的包装器,并且不会影响 C++ 库代码。
关于python - 在python中使用 "using std::vector"时SWIG参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38111942/