常数x不是类型名称,这里x是王
Constant X is not a type name, here x is KING
我对我传递给零件构造函数的国王和颜色的论点遇到了错误。这是我的代码,我正在制作具有班级主的国际象棋游戏,然后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();
}
相关文章:
- 当我从下面的代码中删除关键字 virtual 时,它可以正常工作,否则会出现错误。在这里"virtual"字的意义是什么?
- 在这里,当我们比较 if(vc[i]==vc1[i]) 时,它是向量数组. 实际上比较的值是多少,
- 这里的字符串函数是什么意思
- 模板类是这里的最佳选择吗?如果是,我该怎么做?
- 为什么枚举变量在这里是右值?
- 这里的以下 OOP 结构是什么?
- C++ 互斥锁可以交叉方法/变量吗?如果是这样,为什么在这里不起作用?
- MyClass obj = MyClass();'MyClass()'在这里指的是临时对象吗?
- 使用大括号实例化 - 它是什么,为什么在这里使用它?
- 为什么默认的构造函数在这里不是 constexpr
- 新手在这里,到底什么是 acquireLock(),它的目的是什么?
- "used after it was moved [bugprone-use-after-move]"警告在这里是一个真正的问题吗?
- 常数x不是类型名称,这里x是王
- 右值参考在这里是如何工作的
- 继承在这里是如何工作的
- "cin.exceptions"和"exception"在这里是什么意思
- 返回参考资料在这里是如何工作的
- 什么类型的cast在这里是合适的
- 这里是Qt文档中类的分组列表
- SAFE[]在这里是什么意思