Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why am I unable to use a variable when using the map() function?

int main()
{
    unsigned int num = 2;
    string map[num][3] = {{"c","d","s"}, {"A", "u", "p"}};
    for(int y = 0; y < 2; y++)
            {
                for(int x = 0; x < 3; x++)
                {
                    cout << map[y][x];
                }

                cout << endl;
            }
}

I’m using Xcode and it gives me an error saying "Variable-sized object may not be initialized". Am I simply doing it wrong or is the map function unable to take in variables as arguments?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can declare an array only with constant size, which can be deduced
at compile time.

source: variable-sized object may not be initialized c++

SO, change the line:

unsigned int num = 2;

to this:

unsigned const num = 2;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading