【问题标题】:must use 'class' tag to refer to type ... / expected a type specifier errors (C++ chess)必须使用 \'class\' 标签来引用类型 ... / 预期类型说明符错误(C++ 国际象棋)
【发布时间】:2023-02-13 00:47:18
【问题描述】:

这个函数有几处错误,我不知道是什么。

这是主要功能:

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 的派生类?我怎么做?
  • 这没有解决问题,但您不需要在片段验证代码中使用所有这些括号。 if (abs(dest[0] - source[0] == 2 &amp;&amp; abs(dest[1] - source[1]) == 1) 就足够了,而且更容易阅读。

标签: c++ types constructor chess


【解决方案1】:

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

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

让您指定您正在谈论课程。

您问题的 cmets 中提到的作用域枚举是解决问题的方法。改变:

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

到

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

解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-25
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    • 1970-01-01
    相关资源
    最近更新 更多