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

Time:12-17

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?

CodePudding user response:

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;
  • Related