   Multi Include Files
  I know that there are other similar topics about this question, but I think that mine is somehow different. I'm making a Console Application RPG game (:-D) and I'm working with classes. When I try to compile my latest build the "too many include files" fatal error appears. I have 7 files: Main.cpp, Game.h, Game.cpp, Hero.h, Hero.cpp, Monster.h and Monster.cpp. The thing is that I don't know how to access variables from more files at the same time. Here are the sources of all files without variables:Main.cpp#include <iostream>#include <cstdlib>#include <ctime>#include "Game.h"using namespace std;int main(){   // sourcereturn 0;}Game.h#include <iostream>#include <ctime>#include <fstream>using namespace std;struct status // for holding save variables{// source};class Game : public Hero, status{public:/* Constructor */// source/* *//* Main variables */// source/* *//* Variables */// source/* *//* Functions */// source/* */};Game.cpp#include "Game.h"Game::Game(){}void Game::gMenu(){// source}void Game::gStat(){// source}void Game::gFight(){}void Game::gSave(){// source}void Game::gLoad(){// source}Hero.h#include <iostream>#include <string>using namespace std;class Hero{public:/* Constuctor */// source/* *//* */ // source/* *//* Hero variables */// source/* *//* Functions */// source/* */};[/lang]Hero.cpp#include "Hero.h"Hero::Hero(){// source}void Hero::hAttackF(){// source}void Hero::hDefendF(){   // source}void Hero::hLevelF(){// source}Monster.h#include <iostream>using namespace std;class Monster{public:/* Constructor*/// source/* *//* */// source/* *//* Monster variables */// source/* *//* Functions */// source/* */};Monster.cpp#include "Monster.h"Monster::Monster(){// source}void Monster::mAttackF(){   // source}void Monster::mDefendF(){   // source}OK, my intention is I want:    - Hero to access variables and func. from both Game and Monster classes    - Monster to access variables and func. from both Hero and Game classes    - Game to access variables and func. from both Hero and Monster classes    - And in the end to include only (if possible) the Game file that will hold the Hero and Monster variablesIf there is something you don't understand in the source that's probably 'cause I'm writing it (originally) in my mother tongue (Croatian) and I didn't translate it.I really want to make this work so can anyone help me fix this :-(Appreciate any replies I get :-)   
  You didn't give your headers  include guards.   ;  Can you write the code how my program could work?Thanks!   ;  For each header file:#ifndef FILENAME_H#define FILENAME_H// File contents here#endifWhere FILENAME_H is any unique token (And usually just the filename, e.g. #ifndef GAME_H).   ;  OK, I did that in every .h file. Should I now just include the files which variables I need into the file which needs those variables or what??   ;Quote:Original post by ssj4leonCan you write the code how my program could work?Thanks!One question: Have you looked at SiCranes post?Note also that it is important that you give each header file a distinct include guard. Personally, I usually name them after the file they are housed in, attached with a date when I wrote the guard.Example: I have a file "foobar_frob.h". I have created that file at the same time as this post:#ifndef FOOBAR_FROB_H_20090803#define FOOBAR_FROB_H_20090803content#endif // FOOBAR_FROB_H_20090803;  Yes, I have. And the problem is still unresolved. Now when I try to include the files I need like in Game.h:#ifndef GAME_H#define GAME_H#include <iostream>#include <ctime>#include <fstream>#include "Hero.h"#include "Monster.h"using namespace std;struct status{};class Game : public Hero, Monster, status{public:/* Constructor *//* *//* *//* *//* *//* *//* Functions *//* */};#endifIn the compiler it says for the variables in Hero:'hHP' : identifier not foundand for some other variables almost the same but now it's:'hHP' : undeclared identifier;  No, if you're no longer getting the "too many include files" error, that means that problem was fixed. Now you have a new problem to deal with. In any case, the compiler is trying to find an identifier named hHP. None of your code contains an identifier named hHP. Solution: add the identifier.   ;  Side note, but if you're using Visual Studio, and have no need to compile with any other compiler, then I advise *against* using include guards as specified above.  Instead just put the line "#pragma once" at the top of each header file.  Visual Studio is stupid and doesn't optimize normal include guards, so pragma once is MUCH faster on Visual Studio   ;Quote:Original post by SiCraneNo, if you're no longer getting the "too many include files" error, that means that problem was fixed. Now you have a new problem to deal with. In any case, the compiler is trying to find an identifier named hHP. None of your code contains an identifier named hHP. Solution: add the identifier.It does...Quote:Original post by cache_hitSide note, but if you're using Visual Studio, and have no need to compile with any other compiler, then I advise *against* using include guards as specified above.  Instead just put the line "#pragma once" at the top of each header file.  Visual Studio is stupid and doesn't optimize normal include guards, so pragma once is MUCH faster on Visual StudioI'll do it from now on ;-)OK, now, I'll give you the whole source code for every file so you can find and correct the mistakes from the program. Please note that there are functions that I've declared, and "else"es but I haven't put anything in it. There could be some variables that aren't translated but I don't think there are :-)So please if you can help me with this and give me some suggestions about how to make the game better, more understandable, less messy and less in size or something else I'll highly appreciate it. Thanks everyone for your current help :-) And please read the whole code :-)Here they are:Main.cpp#include <iostream>#include <cstdlib>#include <ctime>#include "Game.h"using namespace std;int main(){srand((int)time(0));Game game;return 0;}Game.h#pragma once#include <iostream>#include <ctime>#include <fstream>#include "Hero.h"#include "Monster.h"using namespace std;struct status{string name;int gold;int PT;};class Game : public Hero, Monster, status{public:/* */Game();/* *//* Main variables */int gGold;int gST; // Start timeint gPT; // Playing time/* *//* */int gInput;int gRNum; // Random numberint gRG; // Random gold/* *//* Funkcije */void gMenu();void gInfo();void gFight();void gSave();void gLoad();/* */};Game.cpp#include "Game.h"Game::Game(){cout << "Select an option:\n";cout << "\t 1) New game...\n";cout << "\t 2) Load game...\n";cout << "\t 3) Exit...\n";cin >> gInput;if (gInput == 1){Hero();gPT = (int)time(0);gGold = 1000;cout << "\nEnter your name: ";cin >> hName;cout << "\nWelcome, " << hName << "!\n";gMenu();}else if (gInput == 2){gLoad();gMenu();}else{}}void Game::gMenu(){gPT = ((int)time(0) - gST) / 60;gRNum = rand() % 3;gRG = (rand() % 50) + 1;cout << "\nSelect an option:\n";cout << "\t1) Go further...\n";cout << "\t2) Game information...\n";cout << "\t3) Hero attributes...\n";cout << "\t4) Save game...\n";cout << "\t5) Load game...\n";cout << "\t6) Exit...\n";cin >> gInput;switch (gInput){case 1:switch(gRNum){case 0:cout << "\nYou ran into a monster!\n";gFight();break;case 1:cout << "\nYou found a treasure and picked up " << gRG << " gold!\n";gGold += gRG;break;case 2:cout << "\nYou haven't encountered anything interesting!!\n";break;}gMenu();break;case 2:cout << "\n";gInfo();gMenu();break;case 3:cout << "\n";hAttr();gMenu();break;case 4:gSave();break;case 5:gLoad();break;case 6:break;default:cout << "\nWrong input, you can only enter a number from 1-5!\n";gMenu();break;}}void Game::gInfo(){cout << "\t______________________________\n";cout << "\t\\  Name: " << hName << "\n";cout << "\t | Gold: " << gGold << "\n";cout << "\t/  Playing time: " << gPT << " minutes\n";cout << "\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";}void Game::gFight(){cout << "\nSelect an option:\n";cout << "\t1) Attack...\t 2) Spells...\n";cout << "\t3) Abilites... 4) Items...\n";cout << "\t5) Defend... \t 6) Run away...\n";cin >> gInput;switch (gInput){case 1:// AttackhAttackF();break;case 2:// Spellsbreak;case 3:// Abilitiesbreak;case 4:// Itemsbreak;case 5:// DefendhDefendF();break;case 6:// Run awaybreak;default:cout << "\nYou can only enter a number from 1-6!\n";gFight();break;}}void Game::gSave(){status DFS; // Data for savingDFS.name = hName;DFS.gold = gGold;DFS.PT = gPT;ofstream save("game.sav", ios::out | ios::binary);if (!save){cout << "\nError opening file for saving\n";}else{cout << "\nThis data will be saved:\n";cout << "Name: " << hName << "\n";cout << "Gold: " << gGold << "\n";cout << "Playing time: " << gPT << " minutes\n";cout << "\nAre you sure you want to save the game?\n";cout << "\t1) Yes...\n";cout << "\t2) No...\n";Input1:cin >> gInput;switch (gInput){case 1:save.write((char *) &DFS, sizeof(status));save.close();cout << "\nThe data is saved!\n";gMenu();break;case 2:cout << "\nAlright. Do you want to continue the game?\n";cout << "\t1) Yes...\n";cout << "\t2) No...\n";Input2:cin >> gInput;switch (gInput){case 1:gMenu();break;case 2:break;default:goto Input2;}break;default:goto Input1;}}}void Game::gLoad(){status DFS;ifstream load("game.sav", ios::in | ios::binary);if (!load){cout << "\nError opening file for loading!\n";}else{load.read((char *) &DFS, sizeof(status));hName = DFS.name;gGold = DFS.gold;gPT = DFS.PT;cout << "\nThis data will be loaded:\n";cout << "Name: " << hName << "\n";cout << "Gold: " << gGold << "\n";cout << "Playing time: " << gPT << " minutes\n";cout << "\nAre you sure you want to load the game?\n";cout << "\t1) Yes...\n";cout << "\t2) No...\n";Input1:cin >> gInput;switch (gInput){case 1:load.write((char *) &DFS, sizeof(status));load.close();cout << "\nThe data is loaded!\n";gMenu();break;case 2:Game();break;}gMenu();}}Hero.h#pragma once#include <iostream>#include <string>#include "Game.h"#include "Monster.h"using namespace std;class Hero : public Game, Monster{public:/* */Hero();/* *//* */ string hName; // Hero's name/* *//* Hero characteristic */unsigned int hHP; // Health pointsunsigned int hFHP; // Full HPint hMP; // Magic pointsint hFMP;int hAttack;int hDefense;int hAgility;int hLevel;int hXP; // Experience pointsint hRXP; // This is the XP that remains if the XP given by the monster is bigger than// the XP needed for hero to achieve next level; this is then subtracted from the next XPint hNLXP; // XP that remains to achieve next levelint hDamage; // Damaga caused by the monsterbool hBDefend; // True if hero defends, false otherwise/* *//* Functions */void hAttr(); // Attributesvoid hAttackF();void hDefendF();void hLevelF();/* */};Hero.cpp#include "Hero.h"Hero::Hero(){hHP = 20;hFHP = 20;hMP = 0;hFMP = 0;hAttack = 10;hDefense = 10;hAgility = 8;hLevel = 1;hXP = 0;hNLXP = 30;}void Hero::hAttackF(){hAgility += (hAgility + ((rand() % 3) + 1));if (hAgility > mAgility){if (mBDefend == true){mDefense *= 1.5;}mDamage = (hAttack - mDefense);if (mDamage >= 1){mHP -= mDamage;if (mHP == 0){mGold = (hLevel * ((rand() % 10) + 1));mXP = ((mHP / 5) * ((mAttack / 5) + (mDefense / 5) + (mAgility / 5)));gGold += mGold;hXP += mXP;hNLXP -= mXP;cout << "\nThe monster is defeated!";cout << "\nYou got " << mGold << " gold!\n";if (hNLXP <= 0){hLevelF();}gMenu();}else{mAttackF();}}else{cout << "\nYou damage has been given!\n";gFight();}}else if (hAgility < mAgility){}else{}}void Hero::hDefendF(){}void Hero::hLevelF(){hRXP = (mXP - hNLXP);hNLXP = ((hXP - hRXP) * 1.5);hLevel++;hFHP += ((rand() % 2) + 4);hFMP += ((rand() % 3) + 2);hAttack += ((rand() % 2) + 3);hDefense += ((rand() % 2) + 3);hAgility += ((rand() % 2) + 2);cout << "\nHero has reached " << hLevel << ". level!";}Monster.h#pragma once#include <iostream>#include "Hero.h"using namespace std;class Monster : public Hero{public:/* */Monster();/* *//* */string mName;/* *//* Monster characteristic */unsigned int mHP; // Health pointsint mMP; // Magic pointsint mAttack;int mDefense;int mAgility;int mXP; // Experience that hero gets after defeating itint mDamage; // Damage caused by heroint mGold; // Gold which the monster drops at the end of the battlebool mBDefend; // True if monster defend, false otherwise/* *//* Functions */void mAttackF();void mDefendF();/* */};Monster.cpp#include "Monster.h"Monster::Monster(){mHP = ((hLevel * ((rand() % 5) + 6)) * 2);mMP = 0;mAttack = (hLevel * ((rand() % 2) + 4));mDefense = (hLevel * ((rand() % 3) + 3));mAgility = (hLevel * ((rand() % 2) + 5));}void Monster::mAttackF(){}void Monster::mDefendF(){}