#include 和可能的循环引用

发布于 2024-12-17 16:09:43 字数 636 浏览 1 评论 0原文

所以我最近的错误开始让我非常烦恼,我浏览了互联网,我想出的最好的解决方案是我有一个周期性的#include错误,但我没有确定到底是什么原因造成的。我的包含结构如下所示:

Player.h -includes-> Pawn.h -includes-> Piece.h -includes-> Player.h

我的意思是,对我来说很明显这是一个循环包含问题,但我不知道如何克服这个问题。让事情变得复杂的是,类 Pawn 扩展了 Piece,并且 Piece 有一个 boost::weak_ptr 返回到 玩家。我的包含内容看起来像这样的原因是因为Player有一个Pawn(和其他Piece)的向量但是 Pawn 还需要调用 Player 的一些方法,所以我给基类 Piece 一个 weak_ptrPlayer代码> 为此。

有什么方法可以更好地设计这个,这样我就没有循环包含?

So my latest error is starting to bug me really bad and I've looked around the internet and the best solution I have come up with is that I have a cyclical #include error, but I'm not sure what is exactly causing that. My include structure looks like the following:

Player.h -includes-> Pawn.h -includes-> Piece.h -includes-> Player.h

I mean, it seems obvious to me that this is a cyclical include problem, but I don't know how to overcome this. To complicate things, class Pawn extends Piece, and Piece has a boost::weak_ptr back to Player. The reason my includes looks like this is because Player has a vector of Pawns (and other Pieces) but Pawn also needs to call some of Player's methods, so I gave the base class Piece a weak_ptr to Player for that.

What is a way that I can design this better so that I don't have a cyclical include?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〆凄凉。 2024-12-24 16:09:43

您可以通过使用前向声明来解决这个问题;事实上,在大多数情况下,您应该更喜欢它们而不是包含标头。

当标头不需要了解另一个类的任何实现细节时,您可以为其使用前向声明,而不是包含整个类定义。这基本上告诉编译器“有一个具有该名称的类”,但仅此而已。

// This is a forward declaration. It tells the compiler that there is a 
// class named Player, but it doesn't know the size of Player or what functions
// it has.
class Player; 

struct Piece {
   // This is just a pointer to player. It doesn't need to know any details about
   // Player, it just needs to know that Player is a valid type.
   boost::weak_ptr<Player> player;
};

作为一般规则,如果文件仅传递对某种类型的指针或引用,则应前向声明该类型。但是,如果它尝试实际使用该类型的对象,则会导致编译器错误。在这种情况下,您需要包含适当的标头。

在大多数情况下,您需要在源文件中包含任何前向声明的类的标头,以便您实际上可以使用所指向的对象。

You can work around this by using forward declarations; in fact, you should prefer them to including headers in most cases.

When a header doesn't need to know about any of the implementation details of another class, you can use a forward declaration for it, instead of including the entire class definition. This basically tells the compiler 'There's a class with this name,' but nothing else.

// This is a forward declaration. It tells the compiler that there is a 
// class named Player, but it doesn't know the size of Player or what functions
// it has.
class Player; 

struct Piece {
   // This is just a pointer to player. It doesn't need to know any details about
   // Player, it just needs to know that Player is a valid type.
   boost::weak_ptr<Player> player;
};

As a general rule, if a file only passes around a pointer or reference to a certain type, that type should be forward declared. However, if it tries to actually use an object of that type, it will result in a compiler error. In this case, you need to include the appropriate header.

In most cases, you'll want to include the headers for any forward declared classes in the source file, so you can actually use the objects that are being pointed to.

最美不过初阳 2024-12-24 16:09:43

在所有头文件中使用 Header Guards ,如下所示:

#ifndef PLAYER_H
#define PLAYER_H

//contents of your header file go here

#endif

In all of your header files use Header Guards that look like this:

#ifndef PLAYER_H
#define PLAYER_H

//contents of your header file go here

#endif
不醒的梦 2024-12-24 16:09:43

将包含内容(如果可能)移至 .cpp 文件。
如果您仍然有循环,则将最常见的内容提取到单独的文件中。
在你的情况下,Player.h 的 Piece.h 包含似乎有问题(只是在这里猜测)。

move includes (if possible) to the .cpp file.
if you still have cycles then extract the stuff the is most common to a separate file.
in your case the Piece.h include for Player.h seems problematic (just guessing here).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文