简体   繁体   中英

C++ error undeclared identifier but Itellisense doesn't show the error

I have :

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

}

I am new to using C++ and I keep getting the error: 'weakClassifiers' : undeclared identifier " at [1] and [2] but intellisense from VS 2012 doesn't have a problem

Please help!

You should use forward declaration . The reason is that when the compiler tries to compile AdaBoost , the word 'WeakClassifier' is still unknown to it.

when you write class WeakClassifier; you tell the compiler: I'm gonna use a type WeakClassifier, I'll define it elsewhere.

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;
};

Beware that in such cases, you can only have a reference or a pointer to that type, like you did with WeakClassifier * inside AdaBoost . ( because the type has not yet been described to the compiler).

You have declared class AdaBoost before the class WeakClassifier in your header file, but your AdaBoost class references the WeakClassifier type when declaring the attribute weakClassifiers . Since the compiler reads your source and included header file sequentially, it can not determine the type for weakClassifiers attribute, that's why it became undeclared.

Put a forward declaration like this before the AdaBoost class:

class WeakClassifier;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM