4

all. I have a class defined as follows:

class Board {
    int columns, rows;
    bool board[10][10];
public:
    Board(int, int);
    void nextFrame();
    void printFrame();
};

My void nextFrame() keeps giving me errors for [rows][columns] because " 'this' cannot be in a constant expression" for both of them. How can I redefine this so that it works? I understand the error. The definition of the function is below, and the error occurs on the 3rd line of the following code sample.

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];
        }
    }
}
yeawhadavit
  • 47
  • 1
  • 3
  • 1
    `bool tempBoard[rows][columns];` -- This is not valid C++ syntax. Arrays must use compile-time constants when declaring the number of entries. – PaulMcKenzie May 16 '16 at 01:10
  • `bool tempBoard[rows][columns];` is a variable length array. An array's dimensions must be constant expressions. That cannot be the case unless your object (class, `this`) is also a compile-time constant. However, if `this` is `const`, then you can't modify `board`... – user6338625 May 16 '16 at 01:10
  • @PaulMcKenzie as opposed to writing "Board TempBoard"? – yeawhadavit May 16 '16 at 01:15
  • @yeawhadavit - Use `std::vector> tempBoard(rows, std::vector(columns));` – PaulMcKenzie May 16 '16 at 01:15
  • Thanks @PaulMcKenzie! – yeawhadavit May 16 '16 at 01:17
  • Why not just typedef the array inside the class and use that same type for your local variable? Vector of vectors is excessive and unnecessary. – paddy May 16 '16 at 01:24
  • @paddy It actually isn't clear what the purpose of the `rows` and `columns` members are if the sizes are constant, so can't say with certainty that a typedef will work. Only the OP can tell us. – PaulMcKenzie May 16 '16 at 01:26
  • @PaulMcKenzie Note that GCC and Clang both accept C's VLAs as an extension, and even handle constructors/destructors correctly. They were temporarily voted into C++14 but removed at the last minute. – o11c May 16 '16 at 05:29
  • @o11c: Note that C++ proposal for array of runtime bound that was included in some working drafts is not the same as C VLAs. – Ben Voigt Jul 27 '22 at 16:00

1 Answers1

1

You need to use a collection from the STL.

Here's an example that nests vectors to get your board:

#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;
}

You can learn about the member initialization syntax for board(vector<vector<bool>>(x, vector<bool>(y))) here and here.

Community
  • 1
  • 1
D3C34C34D
  • 415
  • 2
  • 10