auto关键字不是推断字符串的类型

Roh*_*ain 2 c++ c++11 ubuntu-12.04 g++4.8

我有这个简单的代码:

#include <string>
#include <iostream>

using namespace std;

int main() {
    string s1 = "rohit";
    auto len = s1.size();

    cout << len;
}
Run Code Online (Sandbox Code Playgroud)

当我在Ubuntu 12.04上编译此代码时,它显示以下错误:

test.cc: In function ‘int main()’:
test.cc:8:10: error: ‘len’ does not name a type
     auto len = s1.size();
          ^
test.cc:10:13: error: ‘len’ was not declared in this scope
     cout << len;
             ^
Run Code Online (Sandbox Code Playgroud)

我有g ++ 4.8.1.auto在g ++ 4.8.1中使用关键字是否有一些变化?

Mar*_*k B 5

error: ‘len’ does not name a type使我相信你没有在C++ 11模式下编译,并且它使用了关键字的旧的C++ 98含义auto.