C++程序中增强型for循环编译报错

在程序中使用增强型for循环遍历时,出现了报错: [Error] range-based 'for' loops are not allowed in C++98 mode,编译器提示for循环在C++98是不被允许。

#include <iostream>
#include <vector>
using namespace std;
int main(){
    vector<unsigned> scores(11);
    unsigned int grade;
    while(cin>>grade){
        if(grade<=100)
        	++scores[grade/10]; 	//统计各分数段人数
    }
    for(unsigned i:scores)
        cout<<i<<endl;
    return 0;
}

这是由于range-based 'for' loops是在C++11标准中才被加入,想要解决只需要在编译加上参数 -std=c++0x(或者-std=c++11):工具——编译选项——编译器——编译时加入以下命令;

C++程序中增强型for循环编译报错

确定后,再重新编译即可。