I've been getting an error that says a class isn't declared when I try to pass it to a function. I got this error before, so I deleted the code and typed it out again and it worked. It's not working this time.

The program starts in main, then goes to mainFunctions, then switchForDestinations, then to slotMachineClass which is where it says lotterySystem hasn't been declared (this is just a test for a game I might make). Irrelevant stuff has been removed.

main.cpp:

int main(){
	srand(time(0));
    mainFunctions mainFunction;
    mainFunction.startGame();
    return 0;
}

mainFunctions.cpp:

lotterySystem lottery;
energySystem energy;
moneySystem money;
slotMachineClass slots;

void mainFunctions::startGame(){
   	doSwitchForDestinations();
}

void mainFunctions::doSwitchForDestinations(){
    printText.printPossibleDestinations();
    cout << endl;
    cin >> possibleDestinationChoice;
    cout << endl;
    switch(possibleDestinationChoice){
        case 7:
            switchDestinations.switchCaseSeven(lottery, money, energy, slots);
            break;

}

switchfordestinations:

mainFunctions mainFunction;

void switchForDestinations::switchCaseSeven(lotterySystem& a, moneySystem& b, energySystem& c, slotMachineClass& d){
    int destinationChoice;
    cin >> destinationChoice;
    cout << endl;
    switch(destinationChoice){
        case 1:
            a.playLottery(a, b, c, d);
        case 2:
            d.playSlots(a, b, c, d);
        case 3:
            mainFunction.doSwitchForDestinations();
    }
}

slotMachineClass:

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include "energySystem.h"
#include "healthSystem.h"
#include "intelligenceSystem.h"
#include "printingText.h"
#include "experienceSystem.h"
#include "switchForDestinations.h"
#include "moneySystem.h"
#include "attackSystem.h"
#include "houseSystem.h"
#include "mainFunctions.h"
#include "gameSaving.h"
#include "lotterySystem.h"
#include "slotMachineClass.h"
using namespace std;

switchForDestinations switchDestinations3;

void slotMachineClass::playSlots(lotterySystem& d, moneySystem& a, energySystem& b, slotMachineClass& c){
    //stuff
}

slotMachineClass.h:

#ifndef SLOTMACHINECLASS_H
#define SLOTMACHINECLASS_H

class slotMachineClass{
    public:
        int switchToMoney(int x);
        void playSlots(moneySystem& a, energySystem& b, slotMachineClass& c, lotterySystem &d); //error: 'lotterySystem' has not been declared.
};

#endif // SLOTMACHINECLASS_H

In your main, do you include the needed files? What is the exact error? Make sure, wherever you get an undeclared error, you include the proper files?

In your main, do you include the needed files? What is the exact error? Make sure, wherever you get an undeclared error, you include the proper files?

I included every header file in every cpp file.

Let me see. For example in this snippet you failed to show the includes

#ifndef SLOTMACHINECLASS_H
#define SLOTMACHINECLASS_H

class slotMachineClass{
    public:
        int switchToMoney(int x);
        void playSlots(moneySystem& a, energySystem& b, slotMachineClass& c, lotterySystem &d); //error: 'lotterySystem' has not been declared.
};

#endif // SLOTMACHINECLASS_H

Let me see. For example in this snippet you failed to show the includes

#ifndef SLOTMACHINECLASS_H
#define SLOTMACHINECLASS_H

class slotMachineClass{
    public:
        int switchToMoney(int x);
        void playSlots(moneySystem& a, energySystem& b, slotMachineClass& c, lotterySystem &d); //error: 'lotterySystem' has not been declared.
};

#endif // SLOTMACHINECLASS_H

There are no includes in that file, but if I do put all the includes there, I still get the same error.

I still have this error after 4 weeks :/

The error that I have now is: "Error: 'slotMachineClass' has not been declared."

#ifndef LOTTERYSYSTEM_H
#define LOTTERYSYSTEM_H

#include "moneySystem.h"
#include "energySystem.h"
#include "lotterySystem.h"
#include "slotMachineClass.h"

class lotterySystem{
    private:
        void checkIfAlreadyEntered(int i, int choices[]);
        void checkIfOne(int i);
        int lottoChips;
    public:
        void playLottery(lotterySystem& a, moneySystem& b, energySystem& c, slotMachineClass& d); //I get the error on this line here.
        int checkAnswers(int x[], int y[]);
        int switchToMoney(int x);
        int getLottoChips() const;
        void setLottoChips(int x);
        void changeLottoChips(int x);
};

#endif // LOTTERYSYSTEM_H

I am using Code::Blocks IDE, GNU-GCC compiler. If any other files are needed in fixing the error, please feel free to ask and I will post the code. Ideally I need this error sorted pretty soon... Any help will be appreciated. Thanks.

This article has been dead for over six months. Start a new discussion instead.