【发布时间】:2013-09-19 12:42:36
【问题描述】:
我是游戏开发的菜鸟,我想用 C++ 制作一个简单的平台游戏。问题是当我创建两个类(游戏和图形)时,我不能在 Graphics.h 中包含 Game.h,因为我已经在 Game.h 中包含了 Graphics.h。有谁能够帮我? 代码: 游戏.h:
#pragma once
#include "Graphics.h"
struct Game {
Game();
void init();
void handle();
bool running;
Graphics g;
};
图形.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);
};
【问题讨论】:
-
尽可能避免在 .h 文件中添加#includes,将最常用的头文件包含在一个常见的 .h 文件中(我称之为
PCH.h)并将该头文件包含在启动每个 cpp 文件并从 IDE 或 make 脚本打开预编译头文件。可能看起来很复杂,但从长远来看会为您省去很多麻烦 -
没有理由真的每个类都有一个标题
-
小心这种常见的标题思想:根据我的经验,它会迅速失控,最终会导致大量不必要的依赖。一般来说,包括你需要的,并且只包括你需要的。
标签: c++