gpt4 book ai didi

C++ 错误未声明的标识符但 Itellisense 不显示错误

转载 作者:行者123 更新时间:2023-11-30 02:55:49 25 4
gpt4 key购买 nike

我有:

//in AdaBoost.h

#pragma once

#define ITERATIONS_NUMBER 1000 //numar iteratii AdaBoost

class AdaBoost
{
public:
AdaBoost(void);
~AdaBoost(void);
bool train(int* trainData, int* responses,int,int);
float predict(int* sample);
bool save(char filename[]);
bool load(char filename[]);
private:
WeakClassifier * weakClassifiers[ITERATIONS_NUMBER];
float weights[];
};

class WeakClassifier
{
public:
WeakClassifier(void);
WeakClassifier(int*,int*,float*,int,int);
~WeakClassifier(void);
virtual int compute(int* sample);
double getScore(){return score;}
protected:
double score;
};

class DecisionStump :
public WeakClassifier
{
public:
DecisionStump(void);
DecisionStump(int*,int*,float*,int,int);
~DecisionStump(void);

int compute(int *sample)
{
if (s * sample[dimIndex] < s * stump) return -1;
else return 1;
};
private:
int s;
int stump;
int dimIndex;
int length;
int dimensions;
};

//in AdaBoost.cpp

#include "AdaBoost.h"

#include <limits>


AdaBoost::AdaBoost(void)
{
}


AdaBoost::~AdaBoost(void)
{
}


bool AdaBoost::train(int* trainData, int* responses,int length,int dimensions)
{
//initializarea ponderilor
float tmp = 1.0/dimensions;
for(int i = 0;i < dimensions; i++)
{
weights[i] = tmp;
}

for(int t=0;t < ITERATIONS_NUMBER; t++)
{
//normalizare ponderi
double sumWeights = 0.0;
for(int i = 0;i < dimensions; i++)
{
sumWeights += weights[i];
}
for(int i = 0;i < dimensions; i++)
{
weights[i] = weights[i] / sumWeights;
}

//get weak classifier
weakClassifiers[t] = new DecisionStump(trainData,responses,weights,length,dimensions);

//obtine eroarea clasificatorului slab
double score = weakClassifiers[t]->getScore();


}
return false;
}


WeakClassifier::WeakClassifier(int* trainingData,int* results,float* weights, int length,int dimensions)
{
}



DecisionStump::DecisionStump(void)
{
s = 0;
stump = 0;
dimIndex = 0;
length = 0;
dimensions = 0;
}

DecisionStump::DecisionStump(int* trainingData,int* results,float* weights, int length,int dimensions)
{
this->length = length;
this->dimensions = dimensions;

int dimIndexTmp;
int stumpTmp;
int sTemp;

score = DBL_MAX;
double scoreTemp;

for(int i = 0;i < dimensions;i++)
for(int j = 0;j < length;j++)
{
dimIndexTmp = i;
stumpTmp = trainingData[j* dimensions + dimIndexTmp];

sTemp = -1;
scoreTemp = 0.0;
for(int k = 0;k < length;k++)
{
int result;
if (sTemp * trainingData[k* dimensions + dimIndexTmp] < sTemp * stumpTmp) result = -1;
else result = 1;

//verifica daca clasificarea e corecta
if (result != results[k]) scoreTemp += weights[k];
}
if (scoreTemp < score)
{
dimIndex = dimIndexTmp;
stump = stumpTmp;
s = sTemp;
score = scoreTemp;
}

sTemp = 1;
scoreTemp = 0.0;
for(int k = 0;k < length;k++)
{
int result;
if (sTemp * trainingData[k* dimensions + dimIndexTmp] < sTemp * stumpTmp) result = -1;
else result = 1;

//verifica daca clasificarea e corecta
if (result != results[k]) scoreTemp += weights[k];
}
if (scoreTemp < score)
{
dimIndex = dimIndexTmp;
stump = stumpTmp;
s = sTemp;
score = scoreTemp;
}
}
length = 0;

dimensions = 0;

}

我是使用 C++ 的新手,我不断收到错误消息:'weakClassifiers' : undeclared identifier "at [1] and [2] but intellisense from VS 2012 doesn't have a problem

请帮忙!

最佳答案

你应该使用 forward declaration .原因是当编译器尝试编译 AdaBoost 时,“WeakClassifier”一词仍然未知。

当您编写 class WeakClassifier; 时,您告诉编译器:我将使用类型 WeakClassifier,我将在别处定义它。

class WeakClassifier;
class AdaBoost
{
public:
AdaBoost(void);
~AdaBoost(void);
bool train(int* trainData, int* responses,int,int);
float predict(int* sample);
bool save(char filename[]);
bool load(char filename[]);
private:
WeakClassifier * weakClassifiers[ITERATIONS_NUMBER];
float weights[];
};

class WeakClassifier
{
public:
WeakClassifier(void);
WeakClassifier(int*,int*,float*,int,int);
~WeakClassifier(void);
virtual int compute(int* sample);
double getScore(){return score;}
protected:
double score;
};

请注意,在这种情况下,您只能拥有指向该类型的引用或指针,就像您在 AdaBoost 中使用 WeakClassifier * 所做的那样。 (因为尚未向编译器描述该类型)。

关于C++ 错误未声明的标识符但 Itellisense 不显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16133657/

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