Inf*_*ner 4 c++ variables gcc-warning
编译 C++ 文件时收到以下警告:
variables.cpp:10:8: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
int c{2} ;
Run Code Online (Sandbox Code Playgroud)
这是文件:
#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 ;
}
Run Code Online (Sandbox Code Playgroud)
这是命令行:
g++ -std=c++0x -Wall -Wextra -Winit-self -Wold-style-cast -Woverloaded-virtual -Wuninitialized -Wmissing-declarations -Winit-self -ansi -pedantic variables.cpp -o variables
Run Code Online (Sandbox Code Playgroud)
我在 Ubuntu 18.04.1 上使用 g++ 7.4.0 我不想忽略警告但要解决它,谢谢
PS:我试图将 -std=c++0x 更改为 -std=c++11 但它没有改变任何东西
Mav*_*bot 10
-ansi在您的命令中删除,这相当于-std=c++98并会覆盖您之前的 flag -std=c++11。根据C-Dialect-Options,
在 C 模式下,这相当于 -std=c90。在 C++ 模式下,相当于 -std=c++98。
替换-std=c++0x为-std=c++11。
请注意,如果您的编译器支持它,建议使用最新的 c++ 标准,即-std=c++17. 使用较新的 c++ 标准通常会使您的代码更短、更易读且性能更高。