使用"using std :: vector"时出现SWIG参数错误.在python中 [英] SWIG argument error when using "using std::vector" in python

查看:100
本文介绍了使用"using std :: vector"时出现SWIG参数错误.在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; the using statement will not enter C++ code)
  • Add #define vector std::vector to the SWIG interface file (this will only work if vector is never used as std::vector)
  • Copy the declarations from the header file to the SWIG interface file, and change vector to std::vector. This will cause SWIG to generate correct wrappers, and again will not affect the C++ library code.

这篇关于使用"using std :: vector"时出现SWIG参数错误.在python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆