python - 在python中使用 "using std::vector"时SWIG参数错误

标签 python c++ c++11 vector swig

这与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)文件,并更改 vectorstd::vector 。这将导致 SWIG 生成正确的包装器,并且不会影响 C++ 库代码。

关于python - 在python中使用 "using std::vector"时SWIG参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38111942/

相关文章:

python - 如何根据公差更改cv::Mat值

c++ - 零初始化的 std::atomic<T*> 是否保证等同于用 nullptr 初始化的?

c++ - 警告 : function uses 'auto' type specifier without trailing return type

python - 打开多个txt文件并执行相同的操作

device.type 方法上带有 whitesprace 的 monkeyrunner 的 python 脚本

c++ - 如何在 C++ 中表示 IPv6?

c++ - 我的模板类创建错误导致 SOCKET 出错,为什么?

c++ - 模运算符 (%) 给出不同的结果

python - Heroku 应用程序在本地运行但出现 H12 超时错误(使用包)

python - 将数字分解为其他数字