我是第一次使用动态分配,编译器给了我这个我在其他地方找不到的警告:
warning: non-static data member initializers only available with
-std=c++11 or -std=gnu++11
有没有办法让它消失?我应该关心吗? 谢谢!
最佳答案
问题:
这与动态分配无关。
您可能正在使用以下方法之一进行数据成员初始化,这是 C++11 的一部分:
class S
{
int n; // non-static data member
int& r; // non-static data member of reference type
int a[10] = {1, 2}; // non-static data member with initializer (C++11)
std::string s, *ps; // two non-static data members
struct NestedS {
std::string s;
} d5, *d6; // two non-static data members of nested type
char bit : 2; // two-bit bitfield
};
编译器告诉您您正在使用仅存在于 C++11(及更高版本)中的功能(非静态数据成员初始值设定项)。
解决问题:
- 您可以简单地使用
-std=c++11
标志编译您的代码。 - 或者,如果您出于某种原因想要坚持使用旧标准(例如 C++98)(例如,您的目标是某些没有可用的 C++11 编译器的系统),则可以避免使用此功能。
Should I care?
当然,是的。不注意警告可能会导致许多问题,例如溢出和未定义的行为。
关于c++ - 动态分配内存时的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40812535/