错误:未命名类型 [英] ERROR: does not name a type

查看:276
本文介绍了错误:未命名类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c ++中实现一个象棋游戏,一些类是Board和Piece。
有Rook,King,...等继承自Piece的类。

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".

所以一个棋盘是一个2D数组,所以board.h有piece.h包括。

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

对于最近的发展(在实施移动),我想给这些片段访问板。
so in piece.h我包括board.h。

For recent development (on implementing moves) I wanted to give the pieces access to the board. so in piece.h I included 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]


推荐答案

由于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

这篇关于错误:未命名类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆