问题描述
我有此代码
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)};
};
编译器向我发出此警告,因为它在对象Moveset中的私有部分下声明了对象slot1。 / p>
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?
编辑:静态成员初始值设定项和非静态成员初始值设定项之间的区别是什么?
And what is the diifference between a static member initializer and non-static member initializer?
推荐答案
编译器可能允许它作为扩展,在较早的C ++标准中实际上是不允许的。
The compiler probably allows it as an extension, it's really not allowed in the older C++ standards.
要么通过构造函数初始化器列表初始化对象,要么使用编译器告诉您的标志启用C ++ 11。
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;
};
您似乎正在使用之类的东西,所以我不知道如何添加标志,但是通常在项目设置中的某个地方有一个用于编译器及其设置的选项卡,其中您应该能够添加标志(如果已经没有C ++ 11的复选框),只需在其中添加 -std = c ++ 11
。
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.
这篇关于警告:非静态数据成员初始化程序仅可用于-std = c ++ 11或-std = gnu ++ 11?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!