必须使用'class'标记来引用类型... / 预期的类型说明符错误(C++国际象棋)



这个函数有几个问题,我不知道是什么。

下面是主要函数:

void ChessBoard::resetBoard(){
// set start-of-game parameters
gameEnd = false;
turn = White;
capturedPiece = NULL;

// set initial positions to NULL by default
for (map<string, Piece*>::iterator it = Board.begin(); it != Board.end(); it++) {
it->second = NULL;
}
// add white pieces in their initial positions
Board["A1"] = new Rook(White, this);
Board["B1"] = new Knight(White, this);
Board["C1"] = new Bishop(White, this);
Board["D1"] = new Queen(White, this);
Board["E1"] = new King(White, this);
Board["F1"] = new Bishop(White, this);
Board["G1"] = new Knight(White, this);
Board["H1"] = new Rook(White, this);
const string wfile = "ABCDEFGH";
for (char const &c: wfile) {
string notation = file + "2";
Board[notation] = new Pawn(White, this);
}
// add black pieces in their initial positions
Board["A8"] = new Rook(Black, this);
Board["B8"] = new Knight(Black, this);
Board["C8"] = new Bishop(Black, this);
Board["D8"] = new Queen(Black, this);
Board["E8"] = new King(Black, this);
Board["F8"] = new Bishop(Black, this);
Board["G8"] = new Knight(Black, this);
Board["H8"] = new Rook(Black, this);
const string bfile = "ABCDEFGH";
for (char const &c: bfile) {
string notation2 = bfile + "2";
Board[notation2] = new Pawn(Black, this);
}
}

下面是我对Piece的类定义:

#ifndef PIECE_H
#define PIECE_H
#include <iostream>
#include <string>
using namespace std;
enum Colour {White, Black};
enum Type {King, Queen, Rook, Knight, Bishop, Pawn};
class ChessBoard; 
class Piece {
public:
Piece(Colour colour, ChessBoard *board);
virtual ~Piece();
Colour getColour();
Type getType();
void printColour();
void printType();
virtual bool validMove(const string source, const string dest);
bool freeRow(const string source, const string dest);
bool freeColumn(const string source, const string dest);
bool freeDiagonal(const string source, const string dest);

protected:
ChessBoard *board;
Colour colour;
Type type;
};
#endif

及其实现文件的相关部分:

#include "ChessBoard.h"
#include "Piece.h"
#include <iostream>
using namespace std;
Piece::Piece(Colour colour, ChessBoard *board)
: colour(colour), board(board){}
Piece::~Piece(){}
Colour Piece::getColour(){
return colour;
}
Type Piece::getType(){
return type;
}
void Piece::printType(){
switch(type){
case King:
cout << "King";
break;
case Queen:
cout << "Queen";
break;
case Rook:
cout << "Rook";
break;
case Knight:
cout << "Knight";
break;
case Bishop:
cout << "Bishop";
break;
case Pawn:
cout << "Pawn";
break;
}
}
void Piece::printColour(){
switch(colour){
case White:
cout << "White";
break;
case Black:
cout << "Black";
break;
}
}

下面是一个特定棋子的代表性类实现(它们几乎都是相同的):

#include "Knight.h"
#include <iostream>
using namespace std;
Knight::Knight(Colour colour, ChessBoard* board)
: Piece(colour, board){
type = Type::Knight;
}
Knight::~Knight(){}
bool Knight::validMove(const string source, const string dest){
if((abs(dest[0] - source[0]) == 2) && (abs(dest[1] - source[1]) == 1))
return true;
if((abs(dest[0] - source[0]) == 1) && (abs(dest[1] - source[1]) == 2))
return true;
return false;
}

我试过写Knight::Knight等来区分派生的片类和枚举中的片类型,但那不起作用。我的构造器有什么问题吗?既然我这么说了,我迭代字母来放置小兵的方式有什么问题吗?

(我也肯定包括了所有的头文件!)

这里的问题是,虽然您的意图是调用派生的Piece类,但类的名称被您在枚举中定义的属性所隐藏。也就是说,类Rook被enum属性Rook所隐藏。然后它希望你写:

Board["A1"] = new class Rook(White, this);

让你指定你谈论的是类

在你的问题的评论中提到的范围枚举是解决问题的方法。改变:

enum Type {King, Queen, Rook, Knight, Bishop, Pawn};

enum class Type {King, Queen, Rook, Knight, Bishop, Pawn};

解决问题。

最新更新