gpt4 book ai didi

c++ - 访问类成员 C++

转载 作者:行者123 更新时间:2023-11-30 03:33:31 25 4
gpt4 key购买 nike

我有一个棋盘,其中的行和列如下所示。

class ChessBoard
{
public:
       int getWidth() const;
       int getHeight() const;

       PieceType getPiece(int x, int y) const;
       void setPiece(int x, int y, PieceType Piece);
};

然后我把板做成;

ChessBoard& board;

稍后,我希望在两个 for 循环内访问板上的某个图 block ,但我不确定该怎么做。

for(i=0;i<=ColMax, i++){//column1-->columnMax
for(j=0;j<=rowMax-1,j++){//row1-->rowMax

board.PieceType.i.j // Is this the correct way to access a square?

最佳答案

当您初始化电路板时,您做错了。此代码无效:

Board& board;

这将创建对 Board 对象实例的引用。必须初始化引用,因此除非这是一个类成员声明,​​否则当您尝试构建此代码时会收到错误消息。为了创建 ChessBoard 对象的实例,您需要如下代码:

ChessBoard board;

鉴于您描述的 ChessBoard 界面,您想像这样访问单个棋子的拷贝:

PieceType piece = board.getPiece(i, j);

关于c++ - 访问类成员 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42792021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com