C++ 创建具有非常量大小的二维数组,
C++ Creating 2D array with nonconstant size,
我一直在使用 Codeblocks
,我的代码很好。当我切换到 Visual Studio
时,我收到一条错误消息:
"expression must have constant value" -
int board[n][m];
与 C 相比,C++ 标准中没有可变长度数组 (VLA),即 implementation/evnvironemt 取决于您是否可以将它们与编译器一起使用。
您在 CodeBlocks 中走运,在 Visual Studio.
中走运
两者都允许您在 C++ 中使用容器(例如 std::vector
),这是在 C++ 中实现所需内容的推荐方法。
根据 OP 的评论,老师明确希望使用数组。
最有可能的意思是容器 class std::array
,参见 https://en.cppreference.com/w/cpp/container/array .
我一直在使用 Codeblocks
,我的代码很好。当我切换到 Visual Studio
时,我收到一条错误消息:
"expression must have constant value" -
int board[n][m];
与 C 相比,C++ 标准中没有可变长度数组 (VLA),即 implementation/evnvironemt 取决于您是否可以将它们与编译器一起使用。
您在 CodeBlocks 中走运,在 Visual Studio.
中走运
两者都允许您在 C++ 中使用容器(例如 std::vector
),这是在 C++ 中实现所需内容的推荐方法。
根据 OP 的评论,老师明确希望使用数组。
最有可能的意思是容器 class std::array
,参见 https://en.cppreference.com/w/cpp/container/array .