如何解决变量初始化C ++的警告

问题描述 投票:3回答:2

编译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

我不想忽略警告,但要解决它,谢谢

PS:我试图将-std = c ++ 0x更改为= std = c ++ 11,但没有任何改变

c++ variables gcc-warning
2个回答
7
投票
  1. 删除命令中的-ansi,它等效于-std=c++98,并且会与-std=c++11冲突。根据C-Dialect-Options

    在C模式下,这等效于-std = c90。在C ++模式下,它等效于-std = c ++ 98。

发生冲突时,将应用最右边的标志。这就是为什么您的-std=c++11无效。

  1. -std=c++0x替换-std=c++11

0
投票

您在编译命令行中有2个问题:

第一个是编译命令中的-ansi,它隐式地将标准设置为c ++ 98。如果您使用-ansi选项,则会与-std=c++11产生冲突。

第二个是-std=c++0x,您必须将其替换为-std=c++11

© www.soinside.com 2019 - 2024. All rights reserved.