【问题标题】:std::stoi missing in g++ 4.7.2?g++ 4.7.2 中缺少 std::stoi 吗?
【发布时间】:2013-01-22 12:38:38
【问题描述】:

当我尝试使用 std::stoi 并尝试编译它时,我收到错误消息“stoi is not a member of 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;
}

不会编译。但这并没有什么问题。

【问题讨论】:

  • 如果您需要实际帮助,您必须显示实际代码。
  • 代码没问题。你要我显示什么代码?对 std::stoi 的完全有效调用不起作用?

标签: c++ compiler-errors g++ std c++-standard-library


【解决方案1】:

std::stoi()C++11 中的新内容,因此您必须确保使用以下代码进行编译:

g++ -std=c++11 example.cpp

g++ -std=c++0x example.cpp

【讨论】:

  • 也不起作用。此外, -std=c++11 似乎默认启用。我正在处理的代码的编译器警告之一也说明了这一点(注意,上面的代码只是告诉我 stoi 不是 stoi 的成员,所以问题不在于我的代码)。
  • 我将您的代码保存为 example.cpp 并尝试:g++ example.cpp 并得到了您提到的错误。当我添加-std=c++11-std=c++0x 时,它编译并运行得很好。我笔记本电脑上的 g++ 手册页说 gnu++98 是 C++ 的默认方言。
  • 从头开始。现在我知道为什么它不起作用了。必须在指定要编译的源代码的文件名之前设置该标志……我不知道这一点,我也没有在任何地方看到任何指出这一事实的东西。案件结案。
  • 这对我来说也是个新闻,很高兴你把它解决了!
  • “必须设置标志”是什么意思???什么标志?我遇到了类似的问题
【解决方案2】:

对于旧版本的 C++ 编译器不支持 stoi。对于旧版本,您可以使用以下代码 sn-p 将字符串转换为整数。

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int main() {
    string input;
    cin >> input;
    int s = std::atoi(input.c_str());
    cout<<s<<endl;
    return 0;
}

【讨论】:

  • 得到错误:“atoi”不是“g++ -std=c++98”的“std”成员
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-22
  • 2012-11-07
  • 2020-06-04
  • 2016-04-26
  • 1970-01-01
相关资源
最近更新 更多