在g ++ 4.7.2中缺少std :: stoi?

Aer*_*lSP 11 c++ compiler-errors g++ std c++-standard-library

当我尝试使用std :: stoi并尝试编译它时,我收到错误消息"stoi不是std的成员".我从命令行使用g ++ 4.7.2所以它不能是IDE错误,我按顺序包含所有我的包含,而g ++ 4.7.2默认使用c ++ 11.如果有帮助,我的操作系统是Ubuntu 12.10.有没有配置的东西?

#include <iostream>
#include <string>

using namespace std;

int main(){
  string theAnswer = "42";
  int ans = std::stoi(theAnswer, 0, 10);

  cout << "The answer to everything is " << ans << endl;
}
Run Code Online (Sandbox Code Playgroud)

不会编译.但它没有任何问题.

小智 16

std::stoi()C++ 11中的功能所以你必须确保用以下代码编译它:

g++ -std=c++11 example.cpp
Run Code Online (Sandbox Code Playgroud)

要么

g++ -std=c++0x example.cpp
Run Code Online (Sandbox Code Playgroud)

  • 也不起作用.此外,默认情况下,-std = c ++ 11似乎已启用.我正在处理的代码的编译器警告之一说了很多(注意,上面的代码只是告诉我stoi不是stoi的成员,所以问题不在于我的代码). (6认同)
  • 抓住这一切.现在我知道它为什么不起作用了.在指定要编译的源代码的文件名之前必须设置标志...我没有意识到这一点,并且我没有看到任何指出任何地方的事实.案件结案. (3认同)