【问题标题】:'this' cannot be used in a constant expression error (C++)'this' 不能用于常量表达式错误 (C++)
【发布时间】:2016-05-16 01:04:25
【问题描述】:

所有。我有一个类定义如下:

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

【问题讨论】:

  • bool tempBoard[rows][columns]; -- 这不是有效的 C++ 语法。声明条目数时,数组必须使用编译时常量。
  • bool tempBoard[rows][columns]; 是一个可变长度数组。数组的维度必须是常量表达式。除非您的对象(类,this)也是编译时常量,否则情况并非如此。但是,如果thisconst,那么就不能修改board...
  • @PaulMcKenzie 而不是写“Board TempBoard”?
  • @yeawhadavit - 使用std::vector&lt;std::vector&lt;bool&gt;&gt; tempBoard(rows, std::vector&lt;bool&gt;(columns));
  • 谢谢@PaulMcKenzie!

标签: c++ error-handling


【解决方案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&lt;vector&lt;bool&gt;&gt;(x, vector&lt;bool&gt;(y))) herehere 的成员初始化语法。

【讨论】:

    猜你喜欢
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 2020-03-09
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 2023-01-31
    相关资源
    最近更新 更多