使用"using std :: vector"时出现SWIG参数错误.在python中 [英] SWIG argument error when using "using std::vector" in python
问题描述
Regardless of whether or not this is coding practice, I have come across code that looks like this
test.hh
#include <vector>
using std::vector;
class Test
{
public:
vector<double> data;
};
我正在尝试使用以下接口文件使用swig3.0进行此操作
I am trying to swig this using swig3.0 using the following interface file
test.i
%module test_swig
%include "std_vector.i"
namespace std {
%template(VectorDouble) vector<double>;
};
%{
#include "test.hh"
%}
%naturalvar Test::data;
%include "test.hh"
以及以下测试代码
test.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 >'
这可以通过使用test.hh中的使用std :: vector 的更改为使用命名空间std
的或通过使用std :: vector
并将 vector< double>
更改为 std :: vector< double>
.这不是我想要的.
This can be fixed by either changing the using std::vector
in test.hh to using namespace std
or by removing using std::vector
and changing vector<double>
to std::vector<double>
. This is not what I want.
问题是我被照原样获得了这段代码.我不允许进行更改,但是应该仍然可以通过SWIG使python中的所有内容可用.这里发生了什么?
The problem is that I was given this code as is. I am not allowed to make changes, but I am supposed to still make everything available in python via SWIG. What's going on here?
谢谢.
推荐答案
在我看来,SWIG不正确地使用std :: vector; 语句支持.我认为这是一个SWIG错误.我可以想到以下解决方法:
To me, this looks like SWIG does not support the using std::vector;
statement correctly. I think it's a SWIG bug. I can think of the following workarounds:
- 将
using namespace std;
添加到SWIG接口文件(这只会影响包装器的创建方式;using
语句不会输入C ++代码) - 将
#define向量std :: vector
添加到SWIG接口文件中(仅当vector
从未用作std :: vector 代码>)
- 将声明从头文件复制到SWIG接口文件,并将
vector
更改为std :: vector
.这将导致SWIG生成正确的包装程序,并且不会再次影响C ++库代码.
- Add
using namespace std;
to the SWIG interface file (this will only affect the way wrappers are created; theusing
statement will not enter C++ code) - Add
#define vector std::vector
to the SWIG interface file (this will only work ifvector
is never used asstd::vector
) - Copy the declarations from the header file to the SWIG interface file, and change
vector
tostd::vector
. This will cause SWIG to generate correct wrappers, and again will not affect the C++ library code.
这篇关于使用"using std :: vector"时出现SWIG参数错误.在python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!