我创建了一个返回错误代码(ErrCode
枚举)并传递两个输出参数的函数。但是当我打印函数的结果时,我没有在数组中得到正确的值。
// .. some codes here ..
ErrCode err;
short lstCnt;
short lstArr[] = {};
err = getTrimmedList(lstArr, &lstCnt);
// list returned array (for comparison)
for (int i=0; i<lstCnt; ++i)
printf("lstArr[%3d] = %d", i, lstArr[i]);
// .. some codes here ..
getTrimmedList
函数是这样的:
ErrCode getTrimmedList(short* vList, short* vCnt)
{
short cnt;
ErrCode err = foo.getListCount(FOO_TYPE_1, &cnt);
if (NoError!=err) return err;
short* list = new short [cnt];
short total = 0;
for (short i=0; i<cnt; ++i)
{
FooBar bar = foo.getEntryByIndex(FOO_TYPE_1, i);
if (bar.isDeleted) continue;
list[total] = i;
++total;
}
*vCnt = total;
//vList = (short*)realloc(index, sizeof(short)*total);
vList = (short*)malloc(sizeof(short)*total);
memcpy(vList, list, sizeof(short)*total)
// list returned array (for comparison)
for (int i=0; i<lstCnt; ++i)
printf("lstArr[%3d] = %d", i, lstArr[i]);
return NoError;
}
在哪里:
foo
是一个包含对象数组的FooBar
对象foo.getListCount()
返回具有类型的对象的数量FOO_TYPE_1
FOO_TYPE_1
是我们想要获取/列出的对象的类型foo.getEntryByIndex()
返回类型为i
th的FooBar
对象FOO_TYPE_1
bar.isDeleted
是一个标志,指示是否bar
被视为“已删除”
我的错误是什么?
编辑:
抱歉,我复制了错误的行。我在上面发表了评论并输入了正确的行。
编辑 2
我无法控制foo
and的回报bar
。它们的所有函数返回都是ErrCode
,输出是通过参数传递的。