0
我想設置typemap,這樣我們就可以通過一個Python列表來代替非標準向量。swig,從列表到非標準向量
在C++中我有
template<typename T>
class mm_vector
{
void set_mm_vector(const mm_vector * copy);
}
我希望能夠通過Python列表作爲參數,所以我在.i文件規定:
// python list into vec_of_ints: from Python to C++
%typemap(in) AMM::mm_vector<int>*
{
int i;
if (!PyList_Check($input))
{
PyErr_SetString(PyExc_ValueError, "Expecting a list");
return NULL;
}
Py_ssize_t size = PyList_Size($input); //get size of the list
for (i = 0; i < size; i++)
{
PyObject *s = PyList_GetItem($input,i);
if (!PyInt_Check(s))
{
PyErr_SetString(PyExc_ValueError, "List items must be integers");
return NULL;
}
$1->push_back((int)PyInt_AS_LONG(s)); //put the value into the array
}
}
當我嘗試運行這些線路
l=[0,1]
v = mm.vec_of_ints()
v.set_mm_vector(l)
我有以下錯誤:
File "...", line 1295, in set_mm_vector
def set_mm_vector(self, *args): return _pyamt.vec_of_ints_set_mm_vector(self, *args)
ValueError: Expecting a list
我將不勝感激任何建議!