Visual Studio 中的 C++ 错误

标签 c++ arrays visual-studio codeblocks

我在 CodeBlocks 中用 C++ 构建了一个程序,它运行良好。之后我尝试了 MS Visual Studio,它给了我一条错误消息。我不太确定这是不是我的错误,CodeBlocks 只是没有像 VS 那样识别它,还是由不同的编译器引起的假错误。

代码如下:

#include <iostream>
#include <string>
#include <sstream>


using namespace std;



int main(){

    string widthin;
    int width, height;

    cout << "Enter the size of the puzzle (WIDTH HEIGHT) : " << endl;
    getline(cin, widthin);
    stringstream(widthin) >> width >> height;
    if (width == 0 || height == 0 || width >= 10000 || height >= 10000){
        cout << "Value cannot be a zero nor can it be a letter or a number higher than 10000!" << endl;

        return main();
    }

    cout << width << " x " << height << endl << endl;

    const int plotas = width*height;
    int p;

    bool board[2][plotas];

错误出现在最后一行( bool 数组)。它是这样说的: error C2057: 需要常量表达式; error C2466: 无法分配常量大小为 0 的数组;

最佳答案

VLA(可变长度数组)不是标准的 C++。

一些编译器允许将它们作为语言扩展,但它们并不受欢迎。我建议您采用惯用的方法并改用 std::vector

关于Visual Studio 中的 C++ 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29800262/

相关文章:

c++ - 是否可以获取 mmap 的 Linux 源代码和 MapViewOfFile 的 Windows 源代码?

c、二维字符数组和fopen

c - 被调用函数是否应该始终检查 C 中的数组大小?

Python:在列表中添加

c# - 我可以在 app.config 中参数化 log4net Appender 的类型名称吗?

c++ - 如何停止 Visual Studio 2013 自动重新格式化

sql - Visual Studio 数据库项目生成错误

c++ - C++ 上不同类型的实例化

c++ - Google 测试中的 EXPECT_NO_DEATH()

c++ - 与设备文件的异步读写