-1

how to initialize this string array consisting for four elements. Like i know how to initialize but i don't know Where?? I mean where in the classes. Its giving me error in the constructor. Help needed.

class Water :public Element 
{
public:     
    Water () { }    

    Water(string n): Element (n)
    {
        water = n;
        i=-1;
            //Error Message: expected an expression
        Elements [4] = {"Steam", "Mud", "Sea", "Rain"}; 
    }

    string water;
    int i;
    bool elementexists;

    string Elements [4];//how to initialize this and where?
};
Rook
  • 5,734
  • 3
  • 34
  • 43
user3601076
  • 35
  • 3
  • 8

3 Answers3

1

You can initialize it in constructor initialization list:

Water(string n) 
  : Element(n)
  , water(n)
  , i(-1)
  , Elements{"Steam", "Mud", "Sea", "Rain"} // <- Note the curly braces here.
{}

Raw (C style) arrays cannot be assigned directly using assignment operator (=). An std::array<string, 4> Elements could be assigned, but the syntax would be Elements = {"Steam", "Mud", "Sea", "Rain"}.

Juraj Blaho
  • 13,301
  • 7
  • 50
  • 96
  • shouldn't the curly braces be in parenthesis here? e.g.: ..., Elements({"Steam", "Mud", "Sea", "Rain"}) – meandbug May 13 '14 at 11:15
  • VS2013 is happy to compile `Elements = { "Steam", "Mud", "Sea", "Rain" };`, though intellisense raises a warning. GCC 4.7.2 is not, and demands `Elements = {{ "Steam", "Mud", "Sea", "Rain" }};` VS2013 does accept the `{{}}` version, so that would perhaps be the better way to do it? – Rook May 13 '14 at 11:18
  • it compiles for me. thanks I did not know that. with gcc (and -std=c++11) – meandbug May 13 '14 at 11:20
1

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"}.

Rook
  • 5,734
  • 3
  • 34
  • 43
  • 1
    IMO, your second point should be the main point : using the member initializer list is better than doing it in the constructor body. – Chnossos May 13 '14 at 11:12
  • @Chnossos yeah, this is probably true. I was kinda trying to go for the 'change one thing at a time' approach. – Rook May 13 '14 at 11:15
  • Also, [testing](http://coliru.stacked-crooked.com/a/741c99c117159889) with gcc 8.4 shows that doubling the braces is not necessary. – Chnossos May 13 '14 at 11:19
  • @Chnossos yeah, I'm using GCC 4.7, which does demand them. VS2013 seems happy without. Given that the double braces appear to be acceptable, that is perhaps the more generally useable form? I don't know if there are plans to deprecate that syntax in future. – Rook May 13 '14 at 11:20
  • 1
    IIRC the standard does suggest that the extra braces can be elided in this case. So it likely is a GCC bug. – Chnossos May 13 '14 at 11:33
  • @Chnossos that is possible, yes. (and for the possible benefit of other folk, http://stackoverflow.com/questions/11734861/when-can-outer-braces-be-omitted-in-an-initializer-list) – Rook May 13 '14 at 11:40
0
Elements[0] = "Steam";
Elements[1] = "Mud";
Elements[2] = "Sea";
Elements[3] = "Rain";

should be the easiest way.

nh_
  • 2,211
  • 14
  • 24