Thread: Include cycles

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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, 12:44 PM
  3. help with finding lowest number entered
    By volk in forum C++ Programming
    Replies: 12
    Last Post: 03-22-2003, 01: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