简体   繁体   中英

C++ error in visual studio

I was building a program in c++ in CodeBlocks, and it was running fine. Afterwards I've tried out MS Visual Studio, and it gave me an error message. 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.

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). It is says the following: error C2057: expected constant expression; error C2466: cannot allocate an array of constant size 0;

VLAs (variable length arrays) are not standard 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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