It is important to note that this line:
Elements [4] = {"Steam", "Mud", "Sea", "Rain"};
does not mean 'assign these strings to the 4-element long array, Elements'. Elements [4]
refers to a specific item in that array. Given that Elements
is only 4 items long, attempting to write to the fifth item would be bad. To refer to the array as a whole (as you would do for modifying the whole array at once), just use Elements
.
Now, plain old C-style arrays don't support initialising in quite the way you're trying to do there.
I'd recommend using a std::array
... they are somewhat easier to work with than C-style arrays as you are using. If you might have different numbers of elements, you should use std::vector
instead.
class Water :public Element
{
public:
Water () { }
Water(std::string n): Element (n)
{
water = n;
i=-1;
elements = {{"Steam", "Mud", "Sea", "Rain"}};
}
std::string water;
int i;
bool elementexists;
std::array<std::string, 4> elements;
};
or alternatively,
Water(std::string n): Element(n), elements{{"Steam", "Mud", "Sea", "Rain"}}
{
water = n;
i=-1;
}
Note the double braces around the array initialisers; they're required for std::array
by some older compilers. Newer ones may work just fine with a single set, {"Steam", "Mud", "Sea", "Rain"}
.