最近在学习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 }
/**********************************************************************/
可以看到版本已经是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分
解决方案:5分
要强制开启,或在cmake里设置。