For example:
struct Spell
{
int id;
vector<int> cost(4);
};
It says
'Expected parameter declaration'.
Also does this piece of code have a difference?
if (can(vector <int>inv, spell.cost))
{
cout << "CAST " << spell.id << endl;
done = true;
break;
}
This one says
'Expected '(' for function-style cast or type construction'
Could by any chance lend me a hand?
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 ints (i.e cost) as member and allocate 4 integers and default (value)initialize them .
You can fix it by either of the following ways:
struct Spell {
int id;
std::vector<int> cost{ std::vector<int>(4) };
// ^^^ ^^^^
};
struct Spell {
int id;
std::vector<int> cost = std::vector<int>(4); // or decltype(cost)(4)
// ^^^^^^^^^^^^^^^^^^^^^^^^
};
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With