Include cycles

This is a discussion on Include cycles within the C++ Programming forums, part of the General Programming Boards category; Hi! I'm making a state-machine for my game. Let's say I got a Intro-state, a Game-state, and a End-state. They ...

  1. #1
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105

    Include cycles

    Hi!
    I'm making a state-machine for my game.
    Let's say I got a Intro-state, a Game-state, and a End-state.
    They are implemented as classes, and they inherit from a abstract state class.
    Like this(Psuedo code):
    Code:
    In class Intro:
    -if User pressed a button:
        State.nextState(new Game());
    
    
    In class Game:
    -if User pressed a button:
        State.nextState(new End());
    
    In class End:
    -if User pressed a button:
        State.nextState(new Intro());
    So you see, thease classes start up each other in a cycle.
    The above works fine.

    But here's the problem:
    When I try to split each class into a own file, I have to include Intro in the "End" file, include "End" in the "Game" file, and include "Game" in the "Intro" file.
    So when I try to compile and link this stuff, the header-files will start going bananas, cuz they link to each other in cycle.

    I've tried to put the include-statements inside #ifndef SOMECLASS #define SOMECLASS #endif, stuff, but that didnt work either.
    Anyone know how to deal with this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,454
    Use forward declarations when you can, e.g., the Intro class header file could have:
    Code:
    class Game;
    instead of:
    Code:
    #include "game.h";
    You can this forward declaration when the Intro class definition only requires pointers and/or references to the Game class.

    I've tried to put the include-statements inside #ifndef SOMECLASS #define SOMECLASS #endif, stuff, but that didnt work either.
    You should continue to use header inclusion guards, but they prevent duplicate inclusion, not a cycle. An example:
    Code:
    #ifndef INTRO_CLASS_H_
    #define INTRO_CLASS_H_
    
    class Game;
    
    class Intro
    {
    public:
        void nextState(Game* game);
        // ...
    private:
        // ...
    };
    
    #endif
    Last edited by laserlight; 06-30-2008 at 09:18 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You only have to include Intro.h in End.cpp, End.h in Game.cpp and Game.h in Intro.cpp. You don't have to include each header in the other headers.

  4. #4
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    Hmm..I'm sorry laserlight, I didn't get your point..

    EDIT:
    Ah, I see, I know what you mean now, laserlight! Thanks!
    But Daved do got a more "clean" solution, don't you think?

    Daved:
    Yeah, you're right.
    But you see, I've just put the whole thing into the headers, even the implementations of the classes.

    I guess this is what I get for being lazy
    Last edited by Drogin; 06-30-2008 at 09:25 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,454
    But Daved do got a more "clean" solution, don't you think?
    Daved's solution completes my solution. You cannot use the name of another class before it is declared, hence you need the forward declaration in the header. But then you include the header of the other class in the implementation file.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> But Daved do got a more "clean" solution, don't you think?
    Yes, sorry if I wasn't clear, I was adding to laserlight's post.

    >> But you see, I've just put the whole thing into the headers, even the implementations of the classes.
    That's not lazy so much as incorrect. Technically it might work in some cases, but then if you ever #include your header in more than one source file you will get multiple definition errors. Define the class in the header and define its member functions in the source file and you'll have an easier time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. MFC include BS
    By VirtualAce in forum Windows Programming
    Replies: 4
    Last Post: 10-31-2005, 11:44 AM
  3. help with finding lowest number entered
    By volk in forum C++ Programming
    Replies: 12
    Last Post: 03-22-2003, 12:21 PM
  4. #includes don't seem to be working
    By Inquirer in forum C++ Programming
    Replies: 12
    Last Post: 08-05-2002, 05:38 PM
  5. MFC Assertion Failure
    By maxthecat in forum Windows Programming
    Replies: 5
    Last Post: 08-01-2002, 09:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21