Skip to main content
GameDev.net gamedev.net
🔒 Locked

Header File problem.

Started by leggyguy Dec 14, 2001 at 9:29 AM 3 replies 200+ views
Original Post
leggyguy
leggyguy
Hi, I am having a problem with including one or two headers. I have a fairly large program for me, working perfectly. Now I am trying to add some further functionality, and including headers has become a small problem. For example, I have a class called Game. Game has a member class called PlayState. I have #included PlayState.h into Hame.h, so that the Game class can use my PlayState class. And it is working fine. But Now I am wanting to put a Game class into the Playstate class as a member, and that is where I am haveing the trouble. If I do not #include Game.h in Playstate.h, then I get an error because as far as PlayState is concerned, Game doesn''t exist. (which is quite correct and proper, it doesn''t know about the Game class unless we tell it by including Game.h) But when I do #include Game.h in PlayState.h, then I get all sorts of errors, mainly the portions of Game.h which make use of PlayState.h. Now, I need to use a Game class in the PlayState class, and I also need to use a PlayState class in the Game class, so does anyone know where I am going wrong and how I can fix it?
Fruny
Fruny
Adding a declaration such as ''class Game;'' lets you create references and pointers to Game without including the full header holding the definition for Game.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
CaptainJester
CaptainJester
Did make sure that single header inclusion was used.

ie.

#ifndef MYHEADER_H
#define MYHEADER_H
.
.//your header code
.
#endif

---
Make it work.
Make it right.
Make it fast.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
gandolf1212
gandolf1212
Jesters right, make sure you always add those into your headers.
I think there's to much blood in my caffeine system.
Darkor
Darkor
If you''re using MSVC++:

  
//Game.h

#pragma once

class PlayState;

//Game.cpp

#include "PlayState.h"


  
//PlayState.h

#pragma once

class Game;

//PlayState.cpp

#include "Game.h"

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.