初次用Eclipse CDT编写C++程序,遇到标准适用错误。如:
int main(int argc, char **argv) {
vector<string> vs = {"Grieg", "Williams", "Bach", "Handel" };
sort(vs.begin(),vs.end());
}
编译提示错误为:
../main.cpp:38:61: error: in C++98 ‘vs’ must be initialized by constructor, not by ‘{...}’
vector<string> vs = {"Grieg", "Williams", "Bach", "Handel" };
这种初始化方式是C++标准许可的,故应设置Eclipse按C++标准编译。
方法:Project>Properties 增加一个编译选项-std=c++11
参考:stackoverflow.com/questions/17457069/enabling-c11-in-eclipse-juno-kepler-luna-cdt