【问题标题】:To make gcc 4.8.1 support the new "auto" feature in c++11?要使 gcc 4.8.1 支持 c++11 中的新“自动”功能?
【发布时间】: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

标签: c++ c++11 gcc


【解决方案1】:

您需要使用-std=c++11 标志进行编译

例如:

gcc somefile.c -std=c++11

【讨论】:

  • 谢谢。以前用IDE编程,不太懂。
  • 您的 IDE 中可能有一个选项可以在编译代码时添加编译器标志,查找它可能很有用
猜你喜欢
  • 2014-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多