3

I have this code

class Move
{
    public:
        Move()
        {
            name = "";
            type_num = 18;
            power = 0;
            accuracy = 0;
            type = "???";
        }

        Move(string a, int b, int c, int d)
        {
            name = a;
            type_num = b;
            power = c;
            accuracy = d;
            /*lines of code to give type a string variable depending on the value of type_num*/
        }

    private:
        string name, type;
        int type_num, power, accuracy;
};

class Moveset
{
    public:
        Moveset()
        {
        }               
    private:
        Move slot1{"MOVE 1", rand() % 18, 10*(rand() % 15 + 1), 5 * (rand() % 11 + 10)};
};

And the compiler gave me this Warning for declaring the object slot1 under the private section in class Moveset.

464 83  C:\Users\N\Desktop\C++\Poke\Poke.cpp    [Warning] non-static data member initializers only available with -std=c++11 or -std=gnu++11
464 15  C:\Users\N\Desktop\C++\Poke\Poke.cpp    [Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11
464 83  C:\Users\N\Desktop\C++\Poke\Poke.cpp    [Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11

Although it gave me the warning but apparently it didnt affect the programme running. Does it actually affect anything? and what am I doing wrong here?

Edit: And what is the diifference between a static member initializer and non-static member initializer?

nayfaan
  • 158
  • 1
  • 2
  • 13
  • 1
    [C++11 aggregate initialization for classes with non-static member initializers](http://stackoverflow.com/q/27118535/1708801) and [Has the new C++11 member initialization feature at declaration made initialization lists obsolete?](http://stackoverflow.com/q/24149924/1708801) may be helpful. – Shafik Yaghmour Oct 21 '15 at 09:39
  • 3
    Does this answer your question? [Non-static data member initializers only available with -std=c++11 or -std=gnu++11](https://stackoverflow.com/questions/63543205/non-static-data-member-initializers-only-available-with-std-c11-or-std-gnu) – Donald Duck Aug 23 '20 at 09:20
  • I voted to close this question as a duplicate of the newer one and not the other way around because the other question shows exactly what lines of code give that warning which I think makes it more clear. – Donald Duck Aug 23 '20 at 09:22
  • 1
    agreed......... – nayfaan Aug 24 '20 at 10:15

2 Answers2

1

The compiler probably allows it as an extension, it's really not allowed in the older C++ standards.

Either initialize the object through a constructor initializer list, or enable C++11 using the flags the compiler tells you.


Example using a constructor initialize list:

class Moveset
{
public:
    Moveset()
        : slot1{"MOVE 1", rand() % 18, 10*(rand() % 15 + 1), 5 * (rand() % 11 + 10)}
    {
    }
private:
    Move slot1;
};

You seem to be using an IDE of some kind, so I don't know how to add the flags, but usually somewhere in the project settings there is a tab for the compiler and its settings, where you should be able to add flags (if there isn't a checkbox for C++11 already), just add -std=c++11 there.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

You asked:

Edit: And what is the difference between a static member initializer and non-static member initializer?

Not much different.
The intializer to initialize a static member is a static member initializer.
The intializer to initialize a non-static member is a non-static member initializer.

class foo
{    
    static int var2 = 20;  // static member initializer
    int var1 = 10;         // non-static member initializer
};
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • what is the difference between a static/non-static members? – nayfaan Oct 21 '15 at 05:29
  • `static` members are not bound to a specific instance of the class. See http://en.cppreference.com/w/cpp/language/static for further details. – R Sahu Oct 21 '15 at 05:30
  • y would anyone want to do that? – nayfaan Oct 21 '15 at 05:34
  • There too many use cases for `static` data members. One simple example: If you want to have a string that represents the name of the class in log files or user messages, you can define a `static` member. If you have a class `Foo`, you will most likely use the string `"Foo"` for the class name. That name should remain same for all instances of the class. – R Sahu Oct 21 '15 at 05:37