4
allでは使用できません。私は次のようにクラスが定義されている:'this'は定数式エラー(C++)
class Board {
int columns, rows;
bool board[10][10];
public:
Board(int, int);
void nextFrame();
void printFrame();
};
マイvoid nextFrame()
が[rows][columns]
のために私にエラーを与え続けているため、それらの両方のための「『これは』定数式にすることはできません」。これを再定義してどのように動作させることができますか?私はエラーを理解しています。関数の定義は以下のとおりで、次のコードサンプルの3行目でエラーが発生します。
void Board::nextFrame() {
int numSurrounding = 0;
bool tempBoard[rows][columns];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
if ((i + 1) < rows && board[i + 1][j] == true)
{
numSurrounding++;
}
if ((i - 1) >= 0 && board[i - 1][j] == true)
{
numSurrounding++;
}
if ((j + 1) < columns && board[i][j + 1] == true)
{
numSurrounding++;
}
if ((j - 1) >= 0 && board[i][j - 1] == true)
{
numSurrounding++;
}
if ((i + 1) < rows && (j + 1) < columns && board[i + 1][j + 1] == true)
{
numSurrounding++;
}
if ((i + 1) < rows && (j - 1) >= 0 && board[i + 1][j - 1] == true)
{
numSurrounding++;
}
if ((i - 1) >= 0 && (j + 1) < columns && board[i - 1][j + 1] == true)
{
numSurrounding++;
}
if ((i - 1) >= 0 && (j - 1) >= 0 && board[i - 1][j - 1] == true)
{
numSurrounding++;
}
if (numSurrounding < 2 || numSurrounding > 3)
{
tempBoard[i][j] = false;
}
else if (numSurrounding == 2)
{
tempBoard[i][j] = board[i][j];
}
else if (numSurrounding == 3)
{
tempBoard[i][j] = true;
}
numSurrounding = 0;
}
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
board[i][j] = tempBoard[i][j];
}
}
}
'bool tempBoard [rows] [columns];'これは有効なC++構文ではありません。配列数を宣言する場合、配列はコンパイル時定数を使用する必要があります。 – PaulMcKenzie
'bool tempBoard [rows] [columns];は可変長配列です。配列の次元は定数式でなければなりません。これは、あなたのオブジェクト(クラス、 'this')もコンパイル時定数でない限りは当てはまりません。しかし、 'this'が' const'の場合、 'board'を変更することはできません... – user6338625
" Board TempBoard "とは対照的に@PaulMcKenzie? – yeawhadavit