2016-05-16 6 views
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]; 
     } 
    } 
} 
+1

'bool tempBoard [rows] [columns];'これは有効なC++構文ではありません。配列数を宣言する場合、配列はコンパイル時定数を使用する必要があります。 – PaulMcKenzie

+0

'bool tempBoard [rows] [columns];は可変長配列です。配列の次元は定数式でなければなりません。これは、あなたのオブジェクト(クラス、 'this')もコンパイル時定数でない限りは当てはまりません。しかし、 'this'が' const'の場合、 'board'を変更することはできません... – user6338625

+0

" Board TempBoard "とは対照的に@PaulMcKenzie? – yeawhadavit

答えて

1

STLのコレクションを使用する必要があります。

#include <vector> 
#include <iostream> 

using namespace std; 

class Board { 
    int columns, rows; 
    vector<vector<bool>> board; 
public: 
    Board(int x, int y) : board(vector<vector<bool>>(x, vector<bool>(y))) { 
    } 
    void nextFrame() { 
     // Fill in 
    } 
    void printFrame() { 
     // Fill in 
    } 
    size_t xdim() { 
     return board.size(); 
    } 
    size_t ydim() { 
     if (board.size() == 0) { 
      return 0; 
     } 
     return board.at(0).size(); 
    } 
}; 

int main() { 
    Board b(10, 20); 

    cout << "Made the board" << endl; 
    cout << "X: " << b.xdim() << endl; 
    cout << "Y: " << b.ydim() << endl; 
} 

あなたがboard(vector<vector<bool>>(x, vector<bool>(y)))herehereするための部材の初期化構文について学ぶことができます:

はここにあなたのボードを得るためにベクトルをネスト例です。