auto关键字没有推导字符串的类型

auto keyword is not deducing type of string

本文关键字:字符串 类型 关键字 auto      更新时间:2023-10-16

我有一个简单的代码:

#include <string>
#include <iostream>
using namespace std;
int main() {
    string s1 = "rohit";
    auto len = s1.size();
    cout << len;
}

当我在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;
             ^

我有g++4.8.1。在g++4.8.1中auto关键字的使用有什么变化吗?

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