必须使用“class”标记引用类型.../应为类型说明符错误(C++国际象棋)

lf3rwulv  于 2023-02-14  发布在  其他
关注(0)|答案(1)|浏览(99)

这个函数有几个地方出了问题,我找不出是什么。
主要功能如下:

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类和枚举中的piece类型,但是没有用。我的构造函数有什么问题吗?当我写的时候,我迭代字母来放置卒的方式有什么问题吗?
(我也肯定包括了所有的头文件!)

2admgd59

2admgd591#

这里的问题是,当你的意图是调用派生的Piece类时,类的名称被你在枚举中定义的属性隐藏了,也就是说,类Rook被枚举属性Rook隐藏了,然后它想让你写:

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

让你指明你是在谈论这个类。
您问题的注解中提到的scoped枚举是解决问题的方法。更改:

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

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

解决了这个问题。

相关问题