gpt4 book ai didi

header 命名空间中的 C++ typedef 包含项找不到类型

转载 作者:行者123 更新时间:2023-11-28 07:10:00 24 4
gpt4 key购买 nike

我有一个严格用于定义、全局数据、typedef 等的文件...

游戏数据.h

#include <utility>
#include "player.h"

namespace GameData {
enum Color {
BLACK = 0,
WHITE = 1
};
typedef typename std::pair<char, int> Position;
typedef typename std::pair< std::pair<char, int>, std::pair<char, int> > Move;
typedef typename std::pair<Player*, Player*> Players;
};

我有两个问题:

(1) 未找到播放器类 - 尽管我已将其包含在 header 中

#include <string>
#include "board.h"
#include "gamedata.h"

class Player {
public:
virtual ~Player() {}
virtual Move getMove(Board &c) = 0;
};

(2)播放器头部的Move getMove函数无法解析Move

`Move` does not name a type 

我最好的猜测是发生这种情况是因为它是一个循环包含,两者都需要相互包含。

所以我在 gamedata.h 中转发声明 Player

class Player;
namespace GameData {
enum Color {
BLACK = 0,
WHITE = 1
};
typedef typename std::pair<char, int> Position;
typedef typename std::pair< std::pair<char, int>, std::pair<char, int> > Move;
typedef typename std::pair<Player*, Player*> Players;
};

这解决了播放器问题,但现在移动仍然不稳定。

// None work
GameData::Move
Move

代码:

http://pastebin.com/i0GJz6xt

编辑 1

我想改变这个:

    typedef typename std::pair< std::pair<char, int>, std::pair<char, int> > Move;

    typedef typename std::pair<Position, Position> Move;

但我认为它产生了错误,所以我还没有将其还原。

最佳答案

好吧我在搜索 50 次后找到了答案哈哈。当我说我尝试转发声明 class Player 时,我没有删除问题所在的 #include "player.h"

引自 Peter 87:

You have a circular dependency. janitor.h includes lunch.h. lunch.h includes janitor.h, but the header guard prevents janitor.h from being included and lunch.h fails because it depends on what is in janitor.h.

To prevent this problem you can use forward declaration.

Instead of #include "janitor.h" you write class janitor; This will work as long as all you have is pointers or reference to janitor objects. If you use the object you will need to include the header so in the source files you will probably have to include them. Do the same with all your other headers, not just janitor.h. A good rule is to use forward declarations if it's enough and only include if you have to.

来源:http://www.cplusplus.com/forum/general/56475/

关于 header 命名空间中的 C++ typedef 包含项找不到类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21164882/

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