Initializer cannot be specified for a flexible array member

38 Views Asked by At

I'm trying to initialize a const flexible array as a member variable in a class, something like:

class MyListOfStrings
{
    const std::string myStrings[] = 
    {
        "String1",
        "String2",
        "String3"
    }
};

But this generates the following compiler error:

E1049 an initializer cannot be specified for a flexible array member

Specifying a size for the array works fine:

class MyListOfStrings
{
    const std::string myStrings[3] =
    {
        "String1",
        "String2",
        "String3"
    };
};

Alternatively, I can put the flexible array in a namespace and this works fine:

namespace MyListOfStrings
{
    const std::string myStrings[] =
    {
        "String1",
        "String2",
        "String3"
    };
}

Is there a way I can make this work without having to explicitly specify the array size? If not, is there any reason why the compiler can't figure out the size of the array when it's a member in a class?

0

There are 0 best solutions below