GCC编译器升级到4.8.2,编译C++11的新特性还是不支持?!

C++语言 码拜 9年前 (2015-10-22) 1321次浏览
最近在学习C++,看的是《C++ Primer》的第五版,在centos6.2上升级了GCC到4.8.2版本后,程序编译显示不过.提示还说是C++98不允许。作为一个刚开始学者,在此请教:
可以看到版本已经是4.8.2了
/************g++ 版本*************/
[root@bogon 11]# g++ –version
g++ (GCC) 4.8.2
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
/************************************/
/*********** *编译提示 ****************/
associate.cc:23:18: error: ISO C++ forbids declaration of ‘w’ with no type [-fpermissive]
  for(const auto &w : word_count)
                  ^
associate.cc:23:22: error: range-based ‘for’ loops are not allowed in C++98 mode
  for(const auto &w : word_count)
/****************************************/
/**************程序********************/
#include <iostream>
  2 #include <map>
  3 #include <string>
  4 #include <set>
  5 
  6 
  7 using namespace std;
  8 
  9 int main()
 10 {
 11         map<string,size_t> word_count;
 12         string word;
 13         set<string> exclude;
 14 
 15 
 16         while(cin >> word)
 17                 {
 18                         if(word == “quit”)
 19                                 break;
 20                         else if(exclude.find(word) == exclude.end())
 21                                 ++word_count[word];
 22                 }
 23         for(const auto &w : word_count)
 29                 cout << w.first << “: ” << w.second\
 30                 << ((w.second > 1) ? ” times” : ” time”) << endl;
 31 }
/**********************************************************************/
解决方案:15分
加一个编译选项 -std=c++11
解决方案:20分
g++ test.cpp -o test.out -std=c++0x -Wall
最后那个是编译警告,刚开始学打开比较好
gcc目前需要添加-std=c++0x 才能顺利编译c++11新特性
解决方案:5分
要强制开启,或在cmake里设置。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明GCC编译器升级到4.8.2,编译C++11的新特性还是不支持?!
喜欢 (0)
[1034331897@qq.com]
分享 (0)