我有 :
//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;
};
请注意,在这种情况下,您只能拥有对该类型的引用或指针,就像在
WeakClassifier *
中使用AdaBoost
一样。 (因为尚未向编译器描述类型)。