交叉引用和循环依赖.标题包括间接

use*_*563 4 c++ inheritance header circular-dependency cross-reference

placeable.h

#include "selectable.h"

class placeable : selectable
{
..
};
Run Code Online (Sandbox Code Playgroud)

selectable.h

#include "game.h"


class selectable
{
..
};
Run Code Online (Sandbox Code Playgroud)

game.h

#include "placeable.h"

class game
{
...
class placeable* holding;
...
};
Run Code Online (Sandbox Code Playgroud)

基本上placeable.h包括selectable.h,其中包括game.h,其中包括placeable.h.

我能想到的唯一解决方案是将placeable*放在一个新的头文件中,使其成为静态/全局,然后在game.h和selectable.h中包含这个新头文件.

对不起,我在上面的代码中包含了标题保护.我认为很明显.在这种情况下,由于继承,标题保护没有帮助,同样的事情与前向声明一致.

Mar*_*ork 5

如果你必须只包括标题

使用前向声明优先包括:

您只需要包含类Xiff 的标题:

  • 你有一个'X'班的成员
  • 你来自'X'班
  • 您通过值传递类'X'的参数.

否则,前瞻性声明就足够了.

//  -> Don't do this #include "placeable.h"
class     placeable;  // forward declare
                      // Fine if you are using a pointer.

class game
{
    ...
    class placeable* holding;
    ...
};
Run Code Online (Sandbox Code Playgroud)

PS.添加标头防护.