简体   繁体   English

Visual Studio中的C ++错误

[英]C++ error in visual studio

I was building a program in c++ in CodeBlocks, and it was running fine. 我在CodeBlocks中用c ++构建一个程序,它运行正常。 Afterwards I've tried out MS Visual Studio, and it gave me an error message. 之后我尝试了MS Visual Studio,它给了我一个错误信息。 I'm not quite sure if it is my mistake, and CodeBlocks just doesn't recognise it as well as VS does, or is it a fake error caused by a different compiler. 我不太确定这是不是我的错误,CodeBlocks也不像VS那样识别它,或者它是由不同编译器引起的假错误。

Here's the code: 这是代码:

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

The error appears in last line (bool array). 错误出现在最后一行(bool数组)中。 It is says the following: error C2057: expected constant expression; 它表示如下:错误C2057:预期的常量表达; error C2466: cannot allocate an array of constant size 0; 错误C2466:无法分配常量大小为0的数组;

VLAs (variable length arrays) are not standard C++. VLA(可变长度数组)不是标准C ++。

Some compilers allow them as a language extension, but they're frowned upon. 有些编译器允许它们作为语言扩展,但它们不受欢迎。 I suggest you take the idiomatic approach and use std::vector instead. 我建议你采用惯用法并使用std::vector代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM