私はクラスの初めにメンバー関数の1つから宣言したpublic変数にアクセスしたいと思います。しかし、それは範囲外だと言っている。自分のクラスのメンバ関数内からパブリック変数にアクセスする方法は? (C++)
Goban :: Goban(コンストラクタ)は素晴らしいです。意図的に「ボード」と「isWhiteToPlay」の両方の値に正常にアクセスして変更します。
printBoardのコードは、このクラスを呼び出すプログラムで動作しましたが、ここで移動しましたが、コンパイルしようとすると "board"がこのスコープで宣言されませんでした。ボードもisWhiteToPlayもplayMoveと同じ運命を脱出しません。
私はここで間違っていますか?それは公共の変数ですが、私はまだクラスに入っているので、必要はありません。 Gobanの関数内からGobanの変数にアクセスして変更する簡単な方法はありますか?私は関数に渡してからそれを返すために、ボードをポインタとして扱わなければならないことを楽しみにしています。私はそれをすることができると思うが、私はそれほどエレガントな方法がないと想像することはできない。
私はおそらく、私は完全に私がやっているのか理解していないことを示していた、誰かがおそらくすでにこの質問をしましたが、私は、私はちょうど右の検索用語を考え出すていないと思う疑うclass Goban
{
public:
int board[9][9]; // y,x -1 = W +1 = B
int captures[2];
bool isWhiteToPlay; // 0 for Black's turn, 1 for White's turn
Goban();
void printBoard(); // Prints the board to the terminal wherefrom the program is called.
void playMove(int x, int y); // Makes a move for the turn player at the coordinate (x,y). Right now I'm just trying to get it to change the value of the proper coordinate of "board" and not caring if the move is legal.
};
Goban::Goban() // Just initializing everything to zero here for now. Interesting to note that there are no compiling errors in this constructor. But later it complains when I use board and isWhiteToPlay elsewhere. The only difference I can see is that here they're in for loops, and there they're in if clauses. Not sure why that would make a difference, nor how to work around it.
{
captures[0] = 0;
captures[1] = 0;
for (int j = 0; j <= 8; j++)
{
for (int i = 0; i <=8; i++)
{
board[j][i] = 0;
}
}
isWhiteToPlay = 0;
}
void printBoard() // This code worked correctly when it was in the program, but the problem started when I moved it here to the class.
{
for (int j = 0; j <= 8; j++)
{
for (int i = 0; i <= 8; i++)
{
if (board[j][i] == -1)
{ std::cout << " O"; }
else if (board[j][i] == 1)
{ std::cout << " X"; }
else
{ std::cout << " ."; }
}
}
}
void playMove(int x, int y) // Same errors as above; solution is probably the same.
{
if (isWhiteToPlay == 0)
{
board[y][x] = -1;
isWhiteToPlay = 1;
}
else
{
board[y][x] = 1;
isWhiteToPlay = 0;
}
}
ここで間違っている。誰かが問題を理解していれば、適切な検索用語を知るだけで十分です。適切な現存するstackoverflowスレッドへのリンクが歓迎されます。もちろん、私はここでの答えについては不平を言うことはありませんが、ホイールを再発明する必要はありません。
私はそうだと思います。それは私が予想していたよりも簡単でした。ありがとうございました。 編集:はい。それを変えれば、すべてのエラーがうまくいく。ありがとうございました。 – Joshua