Search code examples
c++includecycle

#include and possible cyclical reference


So my latest error is starting to bug me really bad and I've looked around the internet and the best solution I have come up with is that I have a cyclical #include error, but I'm not sure what is exactly causing that. My include structure looks like the following:

Player.h -includes-> Pawn.h -includes-> Piece.h -includes-> Player.h

I mean, it seems obvious to me that this is a cyclical include problem, but I don't know how to overcome this. To complicate things, class Pawn extends Piece, and Piece has a boost::weak_ptr back to Player. The reason my includes looks like this is because Player has a vector of Pawns (and other Pieces) but Pawn also needs to call some of Player's methods, so I gave the base class Piece a weak_ptr to Player for that.

What is a way that I can design this better so that I don't have a cyclical include?


Solution

  • You can work around this by using forward declarations; in fact, you should prefer them to including headers in most cases.

    When a header doesn't need to know about any of the implementation details of another class, you can use a forward declaration for it, instead of including the entire class definition. This basically tells the compiler 'There's a class with this name,' but nothing else.

    // This is a forward declaration. It tells the compiler that there is a 
    // class named Player, but it doesn't know the size of Player or what functions
    // it has.
    class Player; 
    
    struct Piece {
       // This is just a pointer to player. It doesn't need to know any details about
       // Player, it just needs to know that Player is a valid type.
       boost::weak_ptr<Player> player;
    };
    

    As a general rule, if a file only passes around a pointer or reference to a certain type, that type should be forward declared. However, if it tries to actually use an object of that type, it will result in a compiler error. In this case, you need to include the appropriate header.

    In most cases, you'll want to include the headers for any forward declared classes in the source file, so you can actually use the objects that are being pointed to.