gpt4 book ai didi

c++ - 在所有子标题中包含基本标题的问题

转载 作者:搜寻专家 更新时间:2023-10-31 02:04:57 24 4
gpt4 key购买 nike

<分区>

我有一个基类,它在下面的头文件中声明:

#pragma once
#include "StateHandler.hpp"

namespace ta {
class GameState {
public:
// ...
};
} /* ta */

然后,我有两个 child ,长这样:

#pragma once
#include "../GameState.hpp"

namespace ta {
class DefaultState: public ta::GameState {
public:
// ...
};
} /* ta */

#pragma once
#include "../GameState.hpp"

namespace ta {
class WorldMapState: public ta::GameState {
public:
// ...
};
} /* ta */

尝试编译时,出现错误,GameState 未在我包含的两个子项中的第二个中声明。当我从 GameState.hpp 中删除 #pragma once 时,它说 GameSate 被重新定义。我明白为什么会发生这种情况,但我找不到解决该问题的方法。

更新:使用 include-guards 也不起作用。使用 #pragma once 或 include-guards 时出现以下错误:

In file included from /[...]/include/statemachine/gamestates.hpp:2,
from /[...]/include/common.hpp:4,
from /[...]/include/statemachine/gamestates/../StateHandler.hpp:5,
from /[...]/include/statemachine/gamestates/../GameState.hpp:4,
from /[...]/include/statemachine/gamestates/DefaultState.hpp:4,
from /[...]/include/statemachine/gamestates.hpp:1,
from /[...]/include/common.hpp:4,
from /[...]/source/main.cpp:1:
/[...]/include/statemachine/gamestates/WorldMapState.hpp:7:47: error: expected class-name before '{' token
class WorldMapState: public ta::GameState {
^

这是我得到的错误,当我不在 GameState.hpp 中使用 include-guards 或 #pragma once(这个错误出现 3 次) :

In file included from /[...]/include/common.hpp:5,
from /[...]/source/main.cpp:1:
/[...]/include/statemachine/GameState.hpp:4:11: error: redefinition of 'class ta::GameState'
class GameState {
^~~~~~~~~
In file included from /[...]/include/statemachine/gamestates/WorldMapState.hpp:4,
from /[...]/include/statemachine/gamestates.hpp:2,
from /[...]/include/common.hpp:4,
from /[...]/include/statemachine/gamestates/../StateHandler.hpp:5,
from /[...]/include/statemachine/gamestates/../GameState.hpp:1,
from /[...]/include/statemachine/gamestates/DefaultState.hpp:4,
from /[...]/include/statemachine/gamestates.hpp:1,
from /[...]/include/common.hpp:4,
from /[...]/source/main.cpp:1:
/[...]/include/statemachine/gamestates/../GameState.hpp:4:11: note: previous definition of 'class ta::GameState'
class GameState {

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com