'this'不能用于常量表达式错误(C ++)

时间:2016-05-16 01:04:26

标签: c++ error-handling

所有。我的课程定义如下:

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 个答案:

答案 0 :(得分: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的成员初始化语法。