c++ - 'this' cannot be used in a constant expression error (C++)

标签 c++ error-handling

全部。我有一个定义如下的类:

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

我的 void nextFrame() 一直给我 [rows][columns] 的错误,因为对于它们两者来说“'this' 不能在常量表达式中”。我怎样才能重新定义它以使其起作用?我明白这个错误。函数的定义如下,错误发生在以下代码示例的第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];
        }
    }
}

最佳答案

您需要使用来自 STL 的集合。

这是一个嵌套 vector 以获取您的板的示例:

#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 .

关于c++ - 'this' cannot be used in a constant expression error (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37245333/

相关文章:

c++ - 正确计算PTS和DTS同步音视频ffmpeg C++

c++ - 人们是否能够使用自己的实现来劫持已编译的库?

python - 如何在tkinter中将 'text'按钮彼此进行比较?

java - 返回REST API的漂亮错误JSON

c++ - 处理未知容器中所有项目的最有效方法?

c++ - 无法在没有对象的情况下调用成员函数 std::string class::function()

c++ - 在没有共享内存的情况下计算每个扭曲直方图

python - python中间件捕获错误?

swift - 如何返回用于处理错误条件的 Swift 枚举

ios - 脚本在错误 bash ios 上不退出