49

解决了

真正帮助我的是我可以#include .cpp 文件中的标头而不会导致重新定义的错误。


我是 C++ 新手,但我在 C# 和 Java 方面有一些编程经验,所以我可能会遗漏一些 C++ 独有的基本知识。

问题是我真的不知道出了什么问题,我将粘贴一些代码来尝试解释问题。

我有三个类,GameEvents、Physics 和 GameObject。我有他们每个人的标题。GameEvents 有一个 Physics 和一个 GameObjects 列表。物理学有一个游戏对象列表。

我想要实现的是我希望 GameObject 能够访问或拥有一个物理对象。

如果我只是在 GameObject 中 #include "Physics.h",我会得到我理解的“错误 C2111: 'ClassXXX' : 'class' type redifinition”。这就是我认为#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" 我的代码运行得很好。

我非常感谢任何帮助。

4

8 回答 8

144

预处理器是一个程序,它接受您的程序,进行一些更改(例如包含文件(#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

所以现在让我们看看#includemain 中的 s 是如何被扩展的(这和之前的情况完全一样:复制粘贴)

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 文件。

PS请注意,您也有循环#includes。想象一下预处理器将 Physics.h 的代码复制粘贴到 GameObject.h 中,它看到有一个#include "GameObject.h"which 意味着复制GameObject.h到自身中。当您复制时,您再次获得#include "Pysics.h"并且您永远陷入循环。编译器会阻止这种情况,但这意味着您#include的 s 已经完成了一半。

在说如何解决这个问题之前,你应该知道另一件事。

如果你有:

#include "b.h"

class A
{
    B b;
};

然后编译器需要知道关于 的一切b,最重要的是,它有哪些变量等,以便它知道应该放置多少字节来代替bin 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
}
于 2011-11-05T12:39:02.220 回答
5

您在这里有循环引用:Physics.h包括GameObject.h其中包括Physics.h. 您的类Physics使用GameObject*(指针)类型,因此您不需要包含在内GameObject.hPhysics.h只需使用前向声明 - 而不是

#include "GameObject.h" 

class GameObject;   

此外,在每个头文件中放置警卫。

于 2011-11-05T12:26:46.417 回答
4

问题是您GameObject.h没有警卫,因此当您#include "GameObject.h"加入Physics.h其中时,会被包括GameObject.h在内Physics.h

于 2011-11-05T12:19:23.130 回答
4

*.h在您的所有文件或头文件中添加包含保护*.hh(除非您有特定的理由不这样做)。

要了解发生了什么,请尝试获取源代码的预处理形式。使用 GCC,它类似于g++ -Wall -C -E yourcode.cc > yourcode.i(我不知道 Microsoft 编译器是如何做到这一点的)。您还可以询问包含哪些文件,GCC 为g++ -Wall -H -c yourcode.cc

于 2011-11-05T12:19:49.053 回答
4

首先,您也需要在游戏对象上包含守卫,但这不是真正的问题

如果其他东西首先包括physics.h,physics.h 包括gameobject.h,你会得到这样的结果:

class GameObject {
...
};

#include physics.h

class Physics {
...
};

并且 #include Physics.h 由于包含守卫而被丢弃,并且您最终会在声明 Physics 之前声明 GameObject。

但是如果你想让 GameObject 有一个指向物理的指针,那就是个问题,因为对于 htat,物理必须首先声明。

要解决这个循环,您可以改为前向声明一个类,但前提是您只是将它用作以下声明中的指针或引用,即:

#ifndef PHYSICS_H
#define PHYSICS_H

//  no need for this now #include "GameObject.h"

#include <list>

class GameObject;

class Physics
{
private:
    list<GameObject*> objects;
    list<GameObject*>::iterator i;
public:
    void ApplyPhysics(GameObject*);
    Vector2X CheckCollisions(Vector2X, GameObject*);
};

#endif // PHYSICS_H
于 2011-11-05T12:27:34.563 回答
3

在所有头文件中使用包含保护。由于您使用的是 Visual Studio,因此您可以将#pragma once用作所有标题中的第一个预处理器定义。

但是我建议使用经典方法:

#ifndef CLASS_NAME_H_
#define CLASS_NAME_H_

// Header code here

#endif //CLASS_NAME_H_

第二次阅读前向声明并应用它。

于 2011-11-05T12:31:42.447 回答
2

标头保护的目标是避免多次包含同一个文件。但是目前在 C++ 中使用的 header guard 可以改进。目前的后卫是:

#ifndef AAA_H
#define AAA_H

class AAA
{ /* ... */ };

#endif

我的新后卫建议是:

#ifndef AAA_H
#define AAA_H

class AAA
{ /* ... */ };

#else
class AAA;  // Forward declaration
#endif

这解决了 AAA 类需要 BBB 类声明,而 BBB 类需要 AAA 类声明时出现的烦人问题,通常是因为从一个类到另一个类有交叉指针:

// File AAA.h
#ifndef AAA_H
#define AAA_H

#include "BBB.h"

class AAA
{ 
  BBB *bbb;
/* ... */ 
};

#else
class AAA;  // Forward declaration
#endif

//+++++++++++++++++++++++++++++++++++++++

// File BBB.h
#ifndef BBB_H
#define BBB_H

#include "AAA.h"

class BBB
{ 
  AAA *aaa;
/* ... */ 
};

#else
class BBB;  // Forward declaration
#endif

我希望将其包含在自动从模板生成代码的 IDE 中。

于 2019-06-07T15:19:49.647 回答
1

“ #pragma once” ::: 与标头保护具有相同的目的,并且具有更短且不易出错的额外好处。

许多编译器使用 #pragma 指令支持更简单的替代形式的标头保护:“#pragma once” //您的代码在此处

但是,#pragma once 不是 C++ 语言的官方部分,并且并非所有编译器都支持它(尽管大多数现代编译器都支持)。

出于兼容性目的,人们建议坚持使用传统的头部防护。它们的工作量并不多,并且保证在所有兼容的编译器上都得到支持。

于 2020-04-23T05:59:50.247 回答