As mentioned in the member initialization section in cppreference.com:
Non - static data members may be initialized in one of two ways :
In the member initializer list of the constructor.
struct S
{
int n;
std::string s;
S() : n(7) {} // direct-initializes n, default-initializes s
};
Through a default member initializer, which is a brace or equals initializer included in the member declaration and is used if the member is omitted from the member initializer list of a constructor.
struct S
{
int n = 7;
std::string s{ 'a', 'b', 'c' };
S() {} // default member initializer will copy-initialize n,
// list-initialize s
};
As per the above (emphasis mine), your member declaration is wrong.
I assume that, your intention was to have a vector of int
s (i.e cost
) as member and allocate 4
integers and default (value)initialize them .
You can fix it by either of the following ways:
- ✱uniform - braced initialization
struct Spell {
int id;
std::vector<int> cost{ std::vector<int>(4) };
// ^^^ ^^^^
};
- equals initializer
struct Spell {
int id;
std::vector<int> cost = std::vector<int>(4); // or decltype(cost)(4)
// ^^^^^^^^^^^^^^^^^^^^^^^^
};
- provide a constructor and in constructor member initializer
struct Spell {
int id;
std::vector<int> cost;
Spell()
: cost(4) // constructor member initializer
{}
};
✱Since std::vector
has the std::initializer_list
constructor, unfortunately you can not write std::vector<int> cost{ 4 }
, as it will be interpreted, the 4
being a single vector element.