编译 C++ 文件时收到以下警告:
variables.cpp:10:8: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
int c{2} ;
这是文件:
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std ;
int main()
{
int a = 0 ;
int b(1) ;
int c{2} ;
string myString = "I am a string !" ;
cout << a+b+c << endl ;
cout << myString << endl ;
return EXIT_SUCCESS ;
}
这是命令行:
g++ -std=c++0x -Wall -Wextra -Winit-self -Wold-style-cast -Woverloaded-virtual -Wuninitialized -Wmissing-declarations -Winit-self -ansi -pedantic variables.cpp -o variables
我在 Ubuntu 18.04.1 上使用 g++ 7.4.0
我不想忽略警告而是要解决它,
谢谢
PS:我试图将 -std=c++0x 更改为 -std=c++11 但它没有改变任何东西
最佳答案
-ansi
在您的命令中,相当于 -std=c++98
并会覆盖您之前的标志 -std=c++11
.根据 C-Dialect-Options ,In C mode, this is equivalent to -std=c90. In C++ mode, it is equivalent to -std=c++98.
-std=c++0x
与 -std=c++11
. 请注意,如果您的编译器支持,建议使用最新的 c++ 标准
-std=c++17
.使用较新的 c++ 标准通常会使您的代码更短、更易读且性能更高。
关于c++ - 如何解决变量初始化C++的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59967799/