在python中使用"using std::vector"时SWIG参数错误
SWIG argument error when using "using std::vector" in python
这与这个问题非常相关
不管这是不是编码实践,我遇到过这样的代码
test.hh
#include <vector>
using std::vector;
class Test
{
public:
vector<double> data;
};
我正在尝试使用swig3.0使用以下接口文件
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 >'
这可以通过更改测试中的using std::vector
来修复。hh为using namespace std
或去除using std::vector
,将vector<double>
改为std::vector<double>
。这不是我想要的
问题是我得到的代码是这样的。我不允许进行更改,但我应该仍然通过SWIG在python中提供所有可用的内容。这是怎么回事?
对我来说,这看起来像是SWIG不正确地支持using std::vector;
语句。我认为这是一个SWIG bug。我可以想到以下的解决方法:
- 将
using namespace std;
添加到SWIG接口文件中(这只会影响包装的创建方式;using
语句不会进入c++代码) - 将
#define vector std::vector
添加到SWIG接口文件中(这只会在vector
从未用作std::vector
时起作用) - 将头文件中的声明复制到SWIG接口文件中,并将
vector
更改为std::vector
。这将导致SWIG生成正确的包装器,并且不会影响c++库代码。
相关文章:
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在c++中用vector填充一个简单的动态数组
- vector.resize()中的分配错误
- 使用std::vector的OpenCL矩阵乘法
- 在 Java 中使用 std::vector和 SWIG
- SWIG:传递一个 std::vector< std::vector <double> >指向 python 的指针
- 如何<UserDefinedClass>通过 Swig 将带有数据的 std::vector 传递给 Java?
- 迭代从 SWIG 绑定返回的 python 中的 vector<pair<int,int>>
- swig perl typemap(out) std::vector<std::string> 在 perl 中不返回所需的输出
- 如何使用SWIG为C++ std::vector生成Lua迭代器?
- 返回 vector<pair<int,int>> & from c++ 方法到 python 使用 swig typemap 的元组列表
- std::vector to java.util.Vector code generation with swig
- [swig-JavaScript]是否支持std::vector<std::string>*作为输出?
- 如何使用std::vector在PHP中使用SWIG
- SWIG (v1.3.29)生成的c++到Java Vector类不能正常工作
- 如何使用 swig 类型映射将 std::vector<std::p air<std::string, int> > 从 java 返回到 c++
- 使用 SWIG 将C++包装 <vector> 为 python NumPy 数组
- swig:如何使QList可<T>迭代,如std::vector
- SWIG:如何将boost::shared_ptr封装到std::vector
- 在python中使用"using std::vector"时SWIG参数错误