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

2024-09-30 18:14:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用了SWIG,从C++代码中生成了Python库,遇到了以下错误:

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

我已经包括了接口文件std_vector.I和stl.I以及其他一些似乎必要的文件。当我向函数传递一个整数列表时,我得到了上面的错误。在

感谢任何帮助。在


Tags: 文件of代码innewtype错误argument
1条回答
网友
1楼 · 发布于 2024-09-30 18:14:00

这可能有助于:

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

相关问题 更多 >