1

我有 :

//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

请帮忙!

4

2 回答 2

3

您应该使用前向声明。原因是当编译器试图编译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 *inside所做的那样AdaBoost。(因为尚未向编译器描述该类型)。

于 2013-04-21T17:14:10.533 回答
0

您在头文件中的类AdaBoost之前声明了类WeakClassifier,但是您的类在声明属性时AdaBoost引用了类型。由于编译器顺序读取您的源文件和包含的头文件,它无法确定属性的类型,这就是它变得未声明的原因。WeakClassifierweakClassifiersweakClassifiers

在课前放一个这样的前向声明AdaBoost

class WeakClassifier;
于 2013-04-21T17:15:10.953 回答