2
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?

aheze
  • 24,434
  • 8
  • 68
  • 125
John
  • 33
  • 2
  • There is no `map` function here; the code attempts to define an array whose name is `map`. The same problem would occur if the name was `foo`, `bar`, or `billy`. – Pete Becker Dec 17 '21 at 13:46

1 Answers1

1

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;