错误:未命名类型
我正在用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于 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
在
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
: putclass Board;
(with semicolon!) beforeinclude piece.h
.In
piece.h
: putclass Piece;
(with semicolon!) beforeinclude 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 useboard&
andboard*
(but notboard
by value), and then replace#include "board";
withclass board;
. This tells the compiler that a "unknown" board type exists, and it can pass it by pointer and reference. Thenpiece.h
will have no dependencies, and can be included from any file correctly, likeboard.h
.这是一个循环依赖。我最近读到的一个提示是想象一下如何在一个头文件中定义这两个类,以便每个类都了解另一个类;答案是,你不能。
要打破循环依赖,您需要对其中一个类使用前向声明,而不是包含其头文件。而不是
#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 doclass Board;
.您可以使用 pimpl 模式:请参阅 http://www.gotw.ca/gotw/024.htm
You can use the pimpl pattern: See http://www.gotw.ca/gotw/024.htm