我創建了一個函數,返回一個錯誤代碼(ErrCode
枚舉)並傳遞兩個輸出參數。但是當我打印函數的結果時,我沒有在數組中得到正確的值。作爲輸出參數在c + +
// .. 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()
返回一個類型FOO_TYPE_1
bar.isDeleted
的i
日FooBar
對象如果bar
被視爲「已刪除」或不
我的錯誤是什麼?
編輯:
對不起,我抄錯了線。我在上面評論它,並把正確的路線。
編輯2
我沒有過的foo
和bar
回報控制。它們的所有函數返回值都是ErrCode
,輸出通過參數傳遞。
這不是C++代碼,也就是C代碼。對於C++,您應該使用['std :: vector'](http://en.cppreference.com/w/cpp/container/vector)。 –
和'int main'。 – chris