- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
已解决
真正帮助我的是我可以#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/
我如何使用 if 守卫的理解? type Error = String type Success = String def csrfValidation(session:Session,
我试图深入理解 Angular,所以我阅读了 the docs这非常有帮助。 现在我正在研究守卫。我在文档中阅读了此声明。 The router checks the CanDeactivate an
是否可以批量guard's 观看通知? 例如,如果移动了一个子文件夹,watch 会为每个文件发出一个事件。我真正想要的是一个通知,而不是几个,如果有什么变化。 最佳答案 虽然这不是批量更改,因此没有
我正在使用 Guard gem 在开发的某些时候,我只需要跟踪一个特定的文件或几个文件,而不是整个项目。 是否有一些方便的方法来临时跟踪特定文件? 我知道这可以通过修改保护文件来完成,但我认为这不是一
已解决 真正帮助我的是我可以#include .cpp 文件中的 header 而不会导致重新定义的错误。 我是 C++ 新手,但我有一些 C# 和 Java 编程经验,所以我可能会遗漏一些 C++
是否有可能保证在范围退出时完成的变量。 具体来说,我想要一个守卫:在初始化时调用特定函数,并在作用域退出时调用另一个特定函数的东西。 最佳答案 这最好明确地完成: class Guard def
我有一个类型代表我的应用程序的游戏状态,对于这个问题假装它很简单,例如: Game { points :: Int } 我用 State monad 定义我的游戏逻辑。 type GameState
这个问题在这里已经有了答案: Why is GHC complaining about non-exhaustive patterns? (3 个答案) 关闭 7 个月前。 我有以下代码,它定义了一
我知道在头文件中使用 include guards 是为了防止某些东西被定义两次。不过,使用此代码示例完全没问题: foo.c #include #include #include "bar.h"
这个问题在这里已经有了答案: Why is GHC complaining about non-exhaustive patterns? (3 个答案) 关闭 7 个月前。 我有以下代码,它定义了一
我知道 guard 在 Swift 中的作用。我已经阅读了有关使用 guard 或 if let 的问题。但是,guard (condition) else { return } 和if !condi
我正在试验 Xcode 提供的不同分析选项,但是当我在 Diagnostics 选项卡中启用 Guard Malloc 选项并尝试运行时,我收到了这个错误立即崩溃: dyld: could not l
这是我的 canDeactivate 守卫,它可以工作。但是我不想在使用提交按钮时调用守卫。只有当我通过任何其他方式导航时。怎么办? import { Injectable } from '@angu
嗨,这让我发疯。找了半天也没找到解决方法。 如何为 Guardfile 中的所有守卫触发“run_all”。 当我在 shell 中运行“guard”时,我希望它假装所有文件都已更改并触发所有守卫。
我想知道是否有守卫(断言)函数的 golang 命名约定?我用谷歌搜索了一下,但找不到任何确定的东西。我在“The Go Programming Language”一书中读到,使用“必须”前缀是一种常
我正在尝试使用 guard 的 --listen-on带有 vagrant 的选项,如概述 here ,但我无法让它工作。 如果我添加 config.vm.network :forwarded_por
我目前有一个路线守卫,例如 export class EntityGuard implements CanActivate { constructor(private readonly route
我正尝试在 less 中创建一个高度可定制的按钮 mixin。例如,我希望客户能够进入他们的 .less 文件并写入: .my-button { .btn(@bg: #FFF, @font:
这个问题在这里已经有了答案: #pragma once vs include guards? [duplicate] (13 个答案) 关闭 3 年前。 我正在浏览 Implementation d
这个问题在这里已经有了答案: Is #pragma once a safe include guard? (15 个答案) 关闭 3 年前。 我正在开发一个已知只能在 Windows 上运行并在 V
我是一名优秀的程序员,十分优秀!