2017-09-24 31 views
-1

我已經使用SWIG和從C++代碼生成的Python庫和遇到下列錯誤:矢量分配器參數錯誤而使用SWIG生成Python庫

TypeError: in method 'new_SpikeGeneratorFromVector', argument 1 of type 'std::vector< int,std::allocator< int > >'

我已經包括接口文件std_vector.i和STL。我和一些似乎有必要的東西。當我將一個整數列表傳遞給函數時,我得到了上述錯誤。

任何幫助表示讚賞。

+1

請閱讀如何創建[MCVE],然後展示一些代碼來重現問題。我猜你沒有使用'%template'。 –

+0

@MarkTolonen由於幾個保密問題,我無法在這裏發佈我的整個問題,但我應該更多,特別抱歉。但是,問題確實是像你所建議的那樣使用模板。現在它按預期工作。謝謝! – CodeNinja

回答

0

它可以幫助:

/* File : example.i */ 

%module example 

%{ 
#include "example.h" 
%} 

%include "std_vector.i" 
namespace std { 
    %template(IntVector) vector<int>; 
} 

%include "example.h" 

/*example.h*/ 
void my_func(std::vector<int> v) 
{ 
    for (int i=0; i<v.size(; i++)) 
     std::cout<<v[i]<<"\n"; 
} 

/*in runme.py*/ 

import example 
# call with a python list: 
print example.my_func([1, 2, 3, 4]) 
#call with a python tuple: 
print example.my_func((1, 2, 3, 4)) 
# ... or a wrapped std::vector<int> 

v = example.IntVector(4) 
for i in range(len(v)): 
    v[i] = i + 1 
print example.my_func(v)