已解决
真正帮助我的是我可以#include .cpp 文件中的 header 而不会导致重新定义的错误。
我是 C++ 新手,但我有一些 C# 和 Java 编程经验,所以我可能会遗漏一些 C++ 独有的基本知识。
问题是我真的不知道哪里出了问题,我会贴一些代码来尝试解释问题。
我有三个类,GameEvents、Physics 和 GameObject。我有他们每个人的标题。 GameEvents 有一个 Physics 和一个 GameObjects 列表。 Physics 有一个 GameObjects 列表。
我想要实现的是我希望 GameObject 能够访问或拥有一个物理对象。
如果我只是在 GameObject 中 #include "Physics.h"我会得到 “错误C2111:'ClassXXX':'class'类型重新定义”我理解。 这就是我认为#include-guards 会有所帮助的地方,所以我在我的 Physics.h 中添加了一个包含保护,因为这是我想要包含两次的标题。
这就是它的样子
#ifndef PHYSICS_H
#define PHYSICS_H
#include "GameObject.h"
#include <list>
class Physics
{
private:
double gravity;
list<GameObject*> objects;
list<GameObject*>::iterator i;
public:
Physics(void);
void ApplyPhysics(GameObject*);
void UpdatePhysics(int);
bool RectangleIntersect(SDL_Rect, SDL_Rect);
Vector2X CheckCollisions(Vector2X, GameObject*);
};
#endif // PHYSICS_H
但如果我现在像这样在我的 GameObject.h 中#include "Physics.h":
#include "Texture2D.h"
#include "Vector2X.h"
#include <SDL.h>
#include "Physics.h"
class GameObject
{
private:
SDL_Rect collisionBox;
public:
Texture2D texture;
Vector2X position;
double gravityForce;
int weight;
bool isOnGround;
GameObject(void);
GameObject(Texture2D, Vector2X, int);
void UpdateObject(int);
void Draw(SDL_Surface*);
void SetPosition(Vector2X);
SDL_Rect GetCollisionBox();
};
我遇到了多个不明白为什么会出现的问题。 如果我不#include "Physics.h"我的代码运行得很好。
我非常感谢任何帮助。
最佳答案
预处理器是一个程序,它接受您的程序,进行一些更改(例如包含文件 (#include)、宏扩展 (#define) 以及基本上以 #
开头的所有内容)并给出编译器的“干净”结果。
预处理器在看到#include
时是这样工作的:
当你写作时:
#include "some_file"
some_file
的内容几乎可以直接复制粘贴到包含它的文件中。现在,如果你有:
a.h:
class A { int a; };
还有:
b.h:
#include "a.h"
class B { int b; };
还有:
main.cpp:
#include "a.h"
#include "b.h"
你得到:
main.cpp:
class A { int a; }; // From #include "a.h"
class A { int a; }; // From #include "b.h"
class B { int b; }; // From #include "b.h"
现在您可以看到 A
是如何重新定义的。
当你写守卫时,它们变成这样:
a.h:
#ifndef A_H
#define A_H
class A { int a; };
#endif
b.h:
#ifndef B_H
#define B_H
#include "a.h"
class B { int b; };
#endif
那么现在让我们看看 main 中的 #include
是如何被扩展的(这和之前的情况完全一样:复制粘贴)
main.cpp:
// From #include "a.h"
#ifndef A_H
#define A_H
class A { int a; };
#endif
// From #include "b.h"
#ifndef B_H
#define B_H
#ifndef A_H // From
#define A_H // #include "a.h"
class A { int a; }; // inside
#endif // "b.h"
class B { int b; };
#endif
现在让我们跟随预处理器,看看从中产生了哪些“真实”代码。我会一行一行的去:
// From #include "a.h"
评论。忽视!继续:
#ifndef A_H
A_H
定义了吗?不!然后继续:
#define A_H
好的,现在 A_H
已定义。继续:
class A { int a; };
这不是预处理器的东西,所以不要管它。继续:
#endif
之前的if
到此结束。继续:
// From #include "b.h"
评论。忽视!继续:
#ifndef B_H
B_H
定义了吗?不!然后继续:
#define B_H
好的,现在 B_H
已定义。继续:
#ifndef A_H // From
A_H
定义了吗?是的!然后忽略直到对应的#endif
:
#define A_H // #include "a.h"
忽略
class A { int a; }; // inside
忽略
#endif // "b.h"
之前的if
到此结束。继续:
class B { int b; };
这不是预处理器的东西,所以不要管它。继续:
#endif
之前的if
到此结束。
也就是说,在预处理器处理完文件之后,编译器看到的是这样的:
main.cpp
class A { int a; };
class B { int b; };
如您所见,任何可以在同一个文件中两次获得 #include
d 的东西,无论是直接还是间接都需要加以保护。由于 .h
文件总是很可能被包含两次,因此最好保护所有 .h 文件。
附:请注意,您还有循环 #include
。想象一下预处理器将 Physics.h 的代码复制粘贴到 GameObject.h 中,看到有一个 #include "GameObject.h"
这意味着将 GameObject.h
复制到自身中.当你复制时,你再次得到 #include "Pysics.h"
并且你永远被困在一个循环中。编译器会阻止这种情况,但这意味着您的 #include
已经完成了一半。
在说如何解决这个问题之前,你应该知道另一件事。
如果你有:
#include "b.h"
class A
{
B b;
};
然后编译器需要知道关于 b
的一切,最重要的是,它有哪些变量等等,以便它知道应该放置多少字节来代替 b
在 A
中。
但是,如果您有:
class A
{
B *b;
};
那么编译器实际上并不需要知道任何关于 B
的信息(因为指针,无论类型如何都具有相同的大小)。关于B
,它唯一需要知道的就是它的存在!
所以你做了一些叫做“前向声明”的事情:
class B; // This line just says B exists
class A
{
B *b;
};
这与您在头文件中执行的许多其他操作非常相似,例如:
int function(int x); // This is forward declaration
class A
{
public:
void do_something(); // This is forward declaration
}
关于C++ #include 守卫,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8020113/