【发布时间】:2015-05-01 09:55:00
【问题描述】:
我使用 gcc 4.8.1 编译器来编译我的代码,它使用了 auto 的新 c++11 功能。假设 auto 关键字会自动推断数据类型。但是,编译器会抛出一个错误:
*** does not name a type
这是我的代码:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str="If you want to do it, try your best.";
cout<<str<<endl;
for(auto i:str){ //<-error here
cout<<i<<endl;
}
return 0;
}
错误是:
'i' does not name a type.....(of course there are some error message following)
而我的编译器版本是:gcc version 4.8.1(in MingW)
根据gcc文档,这个版本的gcc已经支持c++11, 那为什么会这样呢?
【问题讨论】:
-
使用 -std=c++11 标志编译
-
你用
-std=c++11编译吗? -
运行 g++ 时在命令行中添加
-std=c++11