4

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?

Bob
  • 1,219
  • 1
  • 14
  • 28
  • 2
    It is possible (and not always bad design) that you may have such a situation where you need to hold pointers of a class in its base or a "manager" class. If this is the case, you can use a simple declaration that it exists (and things should work themselves out in link time). For instance, if you do not actually need the header information (i.e. no objects) change `#include "YourClass.h"` to `class YourClass;` – RageD Nov 24 '11 at 06:22
  • 1
    http://stackoverflow.com/search?q=c%2B%2B+cyclical+reference <-- ? – Billy ONeal Nov 24 '11 at 06:23
  • Oh wow! @RageD I thought that I was still supposed to include the file even if I did a forward declaration like that. Once I removed the include in Piece and swapped it for a forward declaration it all just worked out fine. Thanks! – Bob Nov 24 '11 at 06:27

3 Answers3

4

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.

Collin Dauphinee
  • 13,664
  • 1
  • 40
  • 71
1

In all of your header files use Header Guards that look like this:

#ifndef PLAYER_H
#define PLAYER_H

//contents of your header file go here

#endif
Omtara
  • 2,911
  • 2
  • 19
  • 31
0

move includes (if possible) to the .cpp file. if you still have cycles then extract the stuff the is most common to a separate file. in your case the Piece.h include for Player.h seems problematic (just guessing here).

OSH
  • 2,847
  • 3
  • 25
  • 46