使用C++11新特性编译时编译器报错

实验环境:

ubuntu: 14.04.3 
gcc:4.8.4

测试代码如下:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s {"helloworld"};  // 花括号初始化
    
    cout << s << endl;
    
    return 0;
}

在终端输入代码:

g++ test.cpp

报错信息:

test.cpp: In function ‘int main()’:
test.cpp:8:9: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
  string s {"helloworld"};
         ^
test.cpp:8:24: error: in C++98 ‘s’ must be initialized by constructor, not by ‘{...}’
  string s {"helloworld"};

修改输入:

g++ test.cpp -std=c++11

输出结果:

helloworld
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容