1

I'm a noob in game development and i want to make a simple platform game in C++. The problem is whenI make two classes (Game and Graphics) I can't include Game.h in Graphics.h because I have already included Graphics.h in Game.h. Can anybody help me? code: Game.h:

#pragma once

#include "Graphics.h"

struct Game {
    Game();

    void init();
    void handle();

    bool running;

    Graphics g;
};

Graphics.h:

#pragma once

#include <SDL.h>

struct Graphics {
    SDL_Surface* screen;

    void init();

    void rect(SDL_Rect rect, Uint32 color);
    void rect(SDL_Rect rect, int r, int g, int b);
    void rect(int x, int y, int w, int h, Uint32 color);
    void rect(int x, int y, int w, int h, int r, int g, int b);

    void render(Game* game);
};
ionagamed
  • 11
  • 1
  • 3
  • 3
    Avoid as much as possible adding #includes in .h files, put the most used header includes in a common .h file (I call mine PCH.h) and include that header at the start of every cpp file and turn on precompiled headers from your IDE or make script. It might seem complicated, but it will save you a lot of trouble in the long run – Radu Chivu Sep 15 '13 at 07:15
  • no reason really to have one header per class – mcr619619 Sep 15 '13 at 07:34
  • Be careful with this common header idea: in my experience, it rapidly grows out of control and you end up with loads of unnecessary dependencies. In general, include what you need, and only what you need. – juanchopanza Sep 15 '13 at 08:02

2 Answers2

9

You can use a forward declaration here:

#ifndef GRAPHICS_H_ // portable include guards
#define GRAPHICS_H_

#include <SDL.h>

class Game; // forward declaration. Exactly the same as struct Game.

struct Graphics 
{
  // as before
};

#endif

because Graphics doesn't require the definition of Game. You will most likely need to include Game.h in the implementation file of Graphics.

See related post: When to use forward declaration?

Community
  • 1
  • 1
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • I think it's better to be more consistent and use `struct Game;`. – kennytm Sep 15 '13 at 07:08
  • @KennyTM Possibly. It is not the kind of thing I worry about. I added a comment. – juanchopanza Sep 15 '13 at 07:10
  • Nitpick: isn't postfixing the all-caps guard macro with an underscore technically UB? That should be `GRAPHICS_H` instead IIRC. –  Sep 15 '13 at 07:15
  • @H2CO3 I will have a look. I think post-fixing is OK, double underscores bad, leading underscores followed by a capital letter bad. But I could have missed one out. – juanchopanza Sep 15 '13 at 07:17
  • @juanchopanza Yes, double-underscores are definitively a big no-no :) –  Sep 15 '13 at 07:18
  • @H2CO3 and any leading underscores, in the global namespace. But trailing underscores look OK: http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier – juanchopanza Sep 15 '13 at 07:19
1

You could have a single header merging both Game.h and Graphic.h; there is no reason to have one header per class.

If using GCC (then compile with g++ -Wall -g), an advantage of having only one header for the entire project is the ability to precompile your header. See also this answer.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547