tags:

views:

127

answers:

3

I'm building a C++ project in Eclipse using a third party implementation of gcc. The makefile is auto-generated and the object rules are listed in alphabetical order ( I'm not sure if that's relevant ).

I can't seem to create the correct configuration of #include directives and forward declarations so that it compiles. I'm guessing - and its clearly a bad case of Voodoo Chicken Coding. Can someone tell me how I should work out the include order?

The problem is that the classes refer to each other (e.g. Display refers to Texture2D and vice versa ).

I have the following files:

 Display.cpp
 Display.h

Display.h defines the following classes:

class Canvas{ ... };
class Display
{
public:
   void PaintTexture(Texture2D * tex);
   void WriteText(BitmapFont * fnt);
private:
   Canvas * c;
};

and the files:

Texture2D.h
Texture2D.cpp

Texture2D.h defines the following class:

class Texture2D
{
    void PaintTo(Canvas * c);
};

and finally the files:

BitmapFont.h
BitmapFont.cpp

BitmapFont as defined in BitmapFont.h refers to both Canvas and Texture2D;

class BitmapFont
{
public:
    void Paint(Canvas * c);
private:
    Texture2D * tex;
};

main.cpp refers to BitmapFont and Texture2D.

A: 

If you move the Canvas class to a new header, you can avoid the circular dependencies:

  1. Texture2D.h and Display.h
  2. BitmapFont.h and Display.h

Doing so, the dependency tree would become this:

|-- Canvas.h
|   |-- Nothing? :-)
|-- Texture2D.h
|   |-- Canvas.h
|-- BitmapFont.h
|   |-- Canvas.h
|   |-- Texture2D.h
|   |   |-- Canvas.h
|-- Display.h
|   |-- Canvas.h
|   |-- Texture2D.h
|   |   |-- Canvas.h
|   |-- BitmapFont.h
|   |   |-- Canvas.h
|   |   |-- Texture2D.h
|   |   |   |-- Canvas.h

That means:

Texture2D.h:

#include "Canvas.h"

BitmapFont.h:

#include "Texture2D.h"

Display.h:

#include "Texture2D.h"
#include "BitmapFont.h"

Concluding: no circular dependencies.

jweyrich
+1  A: 

Split Canvas into a separate header; and have forward declarations for Texture2D and BitmapFont in Display.h:

**Canvas.h

#ifndef CANVAS_H
#define CANVAS_H

class Canvas {
public:
    Canvas();
};

#endif

Display.h:

#ifndef DISPLAY_H
#define DISPLAY_H
#include "Canvas.h"

class Texture2D; // Forward declaration
class BitmapFont; // Forward decl

class Display
{
public:
   void PaintTexture(Texture2D* tex);
   void WriteText(BitmapFont* fnt);
private:
   Canvas * c;
};
#endif

Texture2D.h:

#ifndef TEXTURE2D_H
#define TEXTURE2D_H
#include "Canvas.h"

class Texture2D
{
    void PaintTo(Canvas * c);
};
#endif

BitmapFont.h:

#ifndef BITMAPFONT_H
#define BITMAPFONT_H

#include "Canvas.h"
#include "Texture2D.h"

class BitmapFont
{
public:
    void Paint(Canvas * c);
private:
    Texture2D * tex;
};
#endif

main.cpp:

#include "BitmapFont.h"
#include "Texture2D.h"

int main() {
    BitmapFont bf;
    Texture2D tex;

    return 0;
}
kartheek
But the problem is that Display also depends on Texture2D and BitmapFont. At least one forward declaration is necessary.
aschepler
@kartheek - are any forward declarations needed?
BeeBand
Yes, you do! I'd have Canvas declaration into a separate Canvas.h; include Canvas in Display.h, Texture2D.h and BitmapFont.h. Additionally, Display.h will need to have forward declarations for Texture2D and BitmapFont:
kartheek
A: 

Since only pointers to objects of these classes are used in your examples, you should be able to use just forward declarations in the header files. You could try declaring class Canvas; in BitmapFont.h and Texture2D.h and both class BitmapFont; and class Texture2D in Display.h. Then make sure to #include all necessary files in each *.cpp unit.

Or as @jweyrich pointed out, you can likely find a solution using some forward declarations and some cross-#includes as well.

A better solution, though more work, would be to move the definition of class Canvas to its own file. Then BitmapFont.h and Texture2D.h could just #include "Canvas.h" and Display.h could #include BitmapFont.h and Texture2D.h.

aschepler
@aschleper, hmm, `BitmapFont` refers to `Texture2D` also.
BeeBand
@BeeBand: If doing it this way with all forward declarations, you'll probably need all three header files included in each *.cpp file. But of course, it depends on what code is actually in those files. If you try compiling and get no compiler errors in the headers, you're making progress. The usual gcc error message meaning you need to add an #include to complete a forward declaration would be something like 'invalid use of incomplete type'.
aschepler
With the new info that BitmapFont.h refers to `Texture2D`, seems like BitmapFont.h ought to include Texture2D.h. Or if Texture2D.h also ends up referring to `BitmapFont`, then at least one of them will need a forward declaration.
aschepler
I try forward declaring `class Canvas;` in BitmapFont.h and I get the error message `forward declaration of `struct Canvas``;
BeeBand