Search code examples
c++structconstructorstdvector

Creating a vector with n elements in a struct


If I just write this code:

std::vector<int> vec(24, 3);

It'll create a vector called vec with 24 elements all equal to 3. But if I have a struct:

struct Day
{
    std::vector<int> days(24, 3);
};

And try to do the exact same thing it doesn't work, why is this?


Solution

  • Syntax would be:

    struct Day
    {
        vector<int> days = vector<int>(24, 3);
    };
    

    You cannot call constructor with () syntax there (to avoid vexing parse) (but can with {}or = /*...*/).