【发布时间】:2026-02-01 08:50:01
【问题描述】:
我正在尝试编译这段代码:
class OthelloState {
public: // constructor
Othello(int r, int c);
/* other stuff */
private: // private data
const int rows;
const int columns;
int board[rows][columns];
}
我一直以:
OthelloState.h:109: error: invalid use of non-static data member 'OthelloState::rows'
OthelloState.h:115: error: from this location
OthelloState.h:115: error: array bound is not an integer constant
OthelloState.h:112: error: invalid use of non-static data member 'OthelloState::columns'
OthelloState.h:115: error: from this location
OthelloState.h:115: error: array bound is not an integer constant
我认为这意味着我必须将rows 和columns 设为静态。但是,如果我将它们设为静态,我将无法使用构造函数进行初始化,就像我必须为这个项目所做的那样......
我还有其他方法可以做到这一点吗?
PS:我知道在真正的奥赛罗棋中,棋盘是一个 8 x 8 的方格……但考虑到计算机在部分 8 x 8 格上生成下一个最佳棋步需要多长时间后,我们是不会玩“真正的”奥赛罗棋盘(即没有预定义的棋盘尺寸)。
【问题讨论】:
-
不是
static,而是“在编译时恒定且可确定”。 -
不需要初始化
rows和columns吗?他们是consts。 -
@muntoo 我在 cpp 文件的构造函数中初始化了它们。
标签: c++ class compilation multidimensional-array