gpt4 book ai didi

c++ - 函数返回指向指针的指针

转载 作者:行者123 更新时间:2023-11-28 03:18:46 24 4
gpt4 key购买 nike

请看下面我的代码:

class SVMClassifier : public LibHAR
{
public:
...
//This is my function returning a pointer to pointer to svm_node structure
svm_node** SVMFeatureExtraction(SkeData* inputData, int* pFrameNum, int* pFeatureNum, double wt);

//This function calls SVMFeatureExtraction
virtual bool FeatureExtraction(SkeData* inputData, const double* dataLabels = NULL, int labelNum = 0); //This function calls SVMFeatureExtraction
...
private:
svm_node** SVMNodes;
int dataNum;
...
}

svm_node** SVMClassifier::SVMFeatureExtraction(SkeData* inputData, int* pFrameNum, int* pFeatureNum, double wt)
{
*pFeatureNum = FEATURENUM;
*pFrameNum = inputData->GetFrameSaved();
svm_node** pNodes = new svm_node*[*pFrameNum];

for (int i = 0; i < *pFrameNum; i++)
{
pNodes[i] = new svm_node[FEATURENUM + 1];
for (int j = 0; j < FEATURENUM / 3; j++)
{
FEATURE_VEC* pVec = new FEATURE_VEC;
if (!CalFeatureVector(inputData, i+1, j+1, pVec, wt))
return NULL;

pNodes[i][j*3].index = j*3 + 1;
pNodes[i][j*3].value = pVec->x;

pNodes[i][j*3 + 1].index = j*3 + 2;
pNodes[i][j*3 + 1].value = pVec->y;

pNodes[i][j*3 + 2].index = j*3 + 3;
pNodes[i][j*3 + 2].value = pVec->z;

delete pVec;
}
pNodes[i][FEATURENUM].index = -1;
pNodes[i][FEATURENUM].value = 0;

}
return pNodes;
}

bool SVMClassifier::FeatureExtraction(SkeData* inputData, const double* dataLabels, int labelNum)
{

CleanNodes();
int n;
SVMNodes = SVMFeatureExtraction(inputData, &dataNum, &n, actWeight); //Error here!
...
}

类方法 FeatureExtraction 调用另一个方法 SVMFeatureExtraction 返回一个指向指针的指针。我认为指针指向的内存是在堆中动态分配的,因为它是由 "new"创建的。但是当我调试程序的时候,SVMFeatureExtraction返回的地址无法成功赋值给SVMNodes(SVMNodes总是"NULL"),虽然pNodes的内容 是正确的。谁能告诉我代码有什么问题?

谢谢。

最佳答案

这可能是个愚蠢的建议,但您绝对确定这部分永远不会发生吗?

if (!CalFeatureVector(inputData, i+1, j+1, pVec, wt))
return NULL;

关于c++ - 函数返回指向指针的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16063869/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com