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

Dani AI

Generated

This is the classic circular-include / missing-forward-declaration problem, not a mysterious compiler bug. 's posted lotterySystem.h actually includes itself and also pulls in slotMachineClass.h; if slotMachineClass.h in turn includes lotterySystem.h, the include-guards stop one header from actually supplying a definition and the other type becomes "not declared." 's prompt to check includes was right on the money.

Practical fixes (apply in this order):

  • Remove any accidental self-#include lines (e.g. #include "lotterySystem.h" inside lotterySystem.h).
  • In headers, replace #include with forward declarations for classes that are only used as pointers or references. Only include the full header in the corresponding .cpp where the implementation needs the complete type.
  • Make sure the function declaration in the header exactly matches the definition in the .cpp (same parameter types and order).
  • Avoid defining globals in multiple translation units; if globals are needed, declare them extern in a header and define them once in a single .cpp.
  • Check include-guard macros are unique and correct.

Example pattern (illustrates the forward-declare + include-in-cpp approach):

/* slot_machine.h */
#ifndef SLOT_MACHINE_H
#define SLOT_MACHINE_H

class Lottery;   // forward declaration
class Money;
class Energy;

class SlotMachine {
public:
    void play(Lottery &lot, Money &money, Energy &energy);
};

#endif
/* slot_machine.cpp */
#include "slot_machine.h"
#include "lottery.h"
#include "money.h"
#include "energy.h"

void SlotMachine::play(Lottery &lot, Money &money, Energy &energy) {
    // implementation uses full types
}

Extra diagnostics: search headers for self-includes, run the preprocessor (g++ -E) to view include order, and watch missing break statements in switch blocks (logic errors rather than compile errors). These steps should resolve the "has not been declared" messages.

Recommended Answers

All 8 Replies

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.

Has anyone got any other ideas as to why it is not working?

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.

Bump...?

Bump

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.