错误:未命名类型

发布于 2024-12-10 11:37:22 字数 468 浏览 0 评论 0原文

我正在用 C++ 实现一个国际象棋游戏,其中一些类是“棋盘”和“棋子”。 有类“Rook”、“King”等继承自“Piece”。

因此,棋盘是棋子的二维数组,因此 board.h 包含了piece.h。

对于最近的开发(关于实施动作),我想让棋子访问棋盘。 所以在piece.h中我包含了board.h。

这现在会产生上述错误。

一些代码:

//Function in piece.h
#include "board.h"
bool Piece::move( int toX, int toY, bool enemyPawn, const Board & b )
Error: Board does not name a type
Error: ISO C++ forbids declaration of ‘b’ with no type [-fpermissive]

I'm implementing a chess game in c++ and some of the classes are "Board" and "Piece".
There are classes "Rook", "King",... etc inherited from "Piece".

So a board is a 2D array of pieces and so board.h has piece.h included.

For recent development (on implementing moves) I wanted to give the pieces access to the board.
so in piece.h I included board.h.

This now creates above error.

Some code:

//Function in piece.h
#include "board.h"
bool Piece::move( int toX, int toY, bool enemyPawn, const Board & b )
Error: Board does not name a type
Error: ISO C++ forbids declaration of ‘b’ with no type [-fpermissive]

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

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

发布评论

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

评论(4

泅人 2024-12-17 11:37:22

由于 move 仅使用引用,因此您绝对可以避免包含 board.h 并仅向前声明您的 Board 类。如果你真的需要让棋盘了解棋盘,让棋盘了解棋盘,你可以向棋盘声明一个接口类(纯抽象),并包含该接口而不是棋盘,确保棋盘从该接口派生 - 马丁刚刚编辑

Since move only uses a reference, you could definitely avoid the inclusion of board.h and only forward declare your class Board. If you really need to have piece know about board and board know about piece, you could declare an interface class to Board (pure abstract) and include that interface instead of the board making sure to make board derive from the interface – Martin just now edit

萌能量女王 2024-12-17 11:37:22

board.h 中:将 class Board;(带分号!)放在 includepiece.h 之前。
piece.h 中:将 class Piece;(带分号!)放在 include board.h 之前。
一般来说,如果我在标头中定义一个类,我会将 class; 放在文件顶部,位于任何包含内容之上

你有所谓的循环依赖。两个标头都尝试相互包含,要么失败(如果有包含防护),要么无限循环(没有防护)。不管怎样,这个“前瞻性声明”应该可以解决问题。

如果类之间的联系如此紧密,以至于无法修复它们,那么您就必须“解开”它们。将piece/bishop/king/etc 的函数定义移动到各自的.cpp 文件(如果还没有),确保标头仅使用board&board* (但不是按值计算的 board),然后将 #include "board"; 替换为 class board;.这告诉编译器存在“未知”板类型,并且可以通过指针和引用传递它。那么,piece.h没有依赖项,并且可以正确包含在任何文件中,例如 board.h

In board.h: put class Board; (with semicolon!) before include piece.h.
In piece.h: put class Piece; (with semicolon!) before include board.h.
In general, if I define a class in a header, I put class <classname>; at the top of the file, above any includes.

You have what's called a circular dependency. Both headers try to include each other, and either fail (if you have include guards) or infinite loop (without guards). Either way, this "forward declaration" should solve the problem.

If the classes are so tied together that this doesn't fix them, you'll have to "untie" them. Move the function definitions for piece/bishop/king/etc to their respective .cpp files (if they weren't already), make sure that the headers only use board& and board* (but not board by value), and then replace #include "board"; with class board;. This tells the compiler that a "unknown" board type exists, and it can pass it by pointer and reference. Then piece.h will have no dependencies, and can be included from any file correctly, like board.h.

゛清羽墨安 2024-12-17 11:37:22

这是一个循环依赖。我最近读到的一个提示是想象一下如何在一个头文件中定义这两个类,以便每个类都了解另一个类;答案是,你不能。

要打破循环依赖,您需要对其中一个类使用前向声明,而不是包含其头文件。而不是#include "board.h",只需执行class Board;

This is a circular dependency. One hint I read recently is to imagine how you would define both classes in a single header file, such that each knew about the other; the answer is, you can't.

To break the circular dependency you need to use a forward declaration for one of the classes, rather than including its header file. Rather than #include "board.h", just do class Board;.

傾城如夢未必闌珊 2024-12-17 11:37:22

您可以使用 pimpl 模式:请参阅 http://www.gotw.ca/gotw/024.htm

You can use the pimpl pattern: See http://www.gotw.ca/gotw/024.htm

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