John
John

Reputation: 33

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?

Upvotes: 2

Views: 132

Answers (1)

ShunnedJeans
ShunnedJeans

Reputation: 97

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;

Upvotes: 1

Related Questions