使用 SWIG 生成的 python 库时向量分配器参数错误

Vector allocator argument error while using SWIG generated python library

我使用 SWIG 并从 C++ 代码生成了一个 python 库,但遇到以下错误:

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

我已经包含了接口文件 std_vector.i 和 stl.i 以及其他一些似乎有必要的文件。当我将整数列表传递给函数时,出现上述错误。

感谢任何帮助。

可能有帮助:

/* 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)