trying to get preprosceor directives correct

im writing a small game in console and i know i have my preprocessor files in the wrong place.

in addition to the classes below there are 2 more Hero and Monster, include their respective header files.

main.cpp
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include "MainGame.h"
using namespace std;

int main()
{
    MainGame game;
    game.initiate();
    return 0;
}


MainGame.h
1
2
3
4
5
6
7
8
#include "Hero.h"
#include "Monster.h"
#include "Battle.h"
class MainGame
{
    public:
    void initiate();
};

MainGame.cpp
1
2
3
4
5
6
7
8
9
#include "MainGame.h"

void MainGame::initiate()
{
    Hero player;
    Monster monster;
    Battle battle;
    battle.mainbattle(player, monster);
}


battle.h
1
2
3
4
5
6
7
8
9
#include <iostream>


class Battle
{
    public:
        void mainbattle(Hero, Monster);

};

battle.cpp
1
2
3
4
5
6
7
#include "Battle.h"

void Battle::mainbattle(Hero, Monster)
{
    std::cout << "you are fighting a monster";
    //battle  takes place here
}


its giving me an error that Hero and monster have not been declared, or declared in this scope,in battle.h.

*Hero and monster are nearly identical class that contain health for each, and then contains its own header file, the issue is not contained in either.
Last edited on
When you try to compile battle.cpp, the preprocessor turns it into this


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

// BEGIN THE PASTE FROM battle.h BY THE PREPROCESSOR
class Battle
{
    public:
        void mainbattle(Hero, Monster);

};
// END THE PASTE FROM battle.h BY THE PREPROCESSOR

void Battle::mainbattle(Hero, Monster)
{
    std::cout << "you are fighting a monster";
    //battle  takes place here
}


See how your code tries to use the classes Hero and Monster? Where in battle.cpp (or the pasted text from battle.h)are they declared? Nowhere, that's where! :)
Last edited on
You haven't mentioned where in the file error occurs.
Also, in battle.h, isn't it supposed to include MainGame.h or something to forward declare the classes Hero and Monster ???
Topic archived. No new replies allowed.