实验环境:
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