常数x不是类型名称,这里x是王

Constant X is not a type name, here x is KING

本文关键字:这里 是王 类型 常数      更新时间:2023-10-16

我对我传递给零件构造函数的国王和颜色的论点遇到了错误。这是我的代码,我正在制作具有班级主的国际象棋游戏,然后tehre是一个游戏类,可以制作板对象,板扮演游戏,并将正方形和件作为组件对象。他们俩都在建立关联关系

 /////////////////////////PIECE HEADER///////////////////
enum PIECETYPE { KING, QUEEN, ROOK, BISHOP, KNIGHT, PAWN };
enum COLOR { BLACK, WHITE };
class Square;//forward declaration
class Piece
{
    PIECETYPE type;
    COLOR color;
    ...};
/////////////////////PIECE CPP////////////////////////
#include "Piece.h"
#include"Square.h"

Piece::Piece()
{
}
Piece::Piece(PIECETYPE typ, COLOR clr){
    type = typ;
    color = clr;
    pSquare = nullptr;
}
//////////////////////BOARD HEADER: COMPOUNDCLASS////////////////////////
#include"Piece.h"``

class Board
{
    Square square[8][8];
    Piece wK(KING, WHITE);
    Piece bK(KING, BLACK);};

在声明类的成员变量时,您不能使用该构造函数语法。编译器不知道您是否试图声明具有名称wK并返回类型Piece的成员函数,在这种情况下,KING应该是第一个参数的类型。

class Board {
    Piece wK(KING, WHITE);  // ERROR: this is declaring wK and using its constructor in ambigous way.
    ...
};

这是这样做的方法:

class Board {
    Piece wK;  // declaring the member wK
    Board() : wk(KING, WHITE) {}  // initialize wK in the Board constructor
    ...
};

另外,在C 11

class Board {
    Piece wK{KING, WHITE};  // Using {} instead of () makes it clear you are not declaring a function
    ...
};

在您的董事会课程中,创建一个像正方形一样的作品集。请参阅下面的CreatePieces()方法:

#include <iostream>
#include <memory>
using namespace std;
enum PIECETYPE { KING, QUEEN, ROOK, BISHOP, KNIGHT, PAWN };
enum COLOR { BLACK, WHITE };
enum RANK { A, B, C, D, E, F, G, H };
class Square
{
public:
    explicit Square() { }
private:
    //COLOR color;
    //RANK rank;
    //int file;
};
class Piece
{
public:
    explicit Piece(PIECETYPE typ, COLOR clr) : type(typ), color(clr) { }
private:
    PIECETYPE type;
    COLOR color;
    Square* pSquare{ nullptr };
};
class Board
{
public:
    explicit Board()
    {
        CreatePieces();
        // Set the pieces in their proper location, etc.
    }
private:
    Square squares[8][8];
    vector<Piece> pieces;
    inline void CreatePieces()
    {
        pieces.emplace_back(Piece(KING, WHITE));
        pieces.emplace_back(Piece(KING, BLACK));
        // etc. for 30 other pieces
    }
};
int main()
{
    unique_ptr<Board> board = make_unique<Board>();
    getchar();
}