【发布时间】:2018-05-28 07:02:19
【问题描述】:
下面是我的代码:
int** myfunction()
{
int **value = new int*[4];
for(int i=0;i<4;i++)
{
value[i] = new int[4];
memset(value[i], 0, 4*sizeof(int));
}
// asign value
return value;
}
然后我想调用myfunction 并使用python 列表返回int ** 类型值,所以我在我的.i 文件中添加了一个类型映射:
%typemap(out) int** {
$result = PyList_New(4);
for(int i=0;i<4;i++)
{
PyObject *o = PyList_New(4);
for(int j=0;j<4;j++)
{
PyList_SetItem(o,j,PyInt_FromLong((long)$1[i][j]));
}
PyList_SetItem($result, i, o);
}
delete $1;
}
我在我的 python 代码中调用 myfunction 却什么也没得到。我的代码有什么不正确的地方?
【问题讨论】:
-
“无”是什么意思?您在 python 脚本中得到“无”?或者你得到一些零值?顺便说一句,SWIG 不支持指针运算,请参阅doc,第 5.3 节。我不确定您要在 python 脚本中做什么,但我希望您不能在 python 脚本中遍历数组。
-
result = myfunction() 在我的 python 中,然后我进行打印 print('result = ', result)。并没有打印任何内容。看起来我的 c++ 函数崩溃了,所以 print 没有运行。我正在尝试在我的 python 脚本中获取 c++ 函数的返回值。我不知道如何在python脚本中获取一个int**返回值。