cygwin g++ std::stoi "error: ‘stoi’ is not a member of ‘std

18

我有:

-在Windows 7/32位上安装了Cygwin 1.7.25

-g++ --version --> g++ (GCC) 4.8.2

-libstdc++.a --> gcc-g++-4.8.2-1

尝试编写一个C++的Hello World程序:

#include <string>

int main() 
{
   std::string s = "123";
   int i = std::stoi(s);
}

编译产生如下结果:

$ g++ -std=c++11 main.cpp
main.cpp: In function ‘int main()’:
main.cpp:6:10: error: ‘stoi’ is not a member of ‘std’
  int i = std::stoi(s);

我搜索了几个小时,但仍然找不到解决方案。这里出了什么问题?


1
奇怪。它应该可以工作... 这似乎是一些奇怪的错误。 - Ali
5个回答

12

1
我也遇到了Cygwin最新稳定版本的此问题,但是使用to_string函数时。 - user753676
cygwin v4.9.2 Windows 7/64 仍然没有 stoi,但 strtol 可以胜任 - G O'Rilla

3

昨天我也遇到了同样的问题: "error: 'stoi' is not a member of 'std'."

首先,我确保启用了c++11。然后,我将g++编译器更新为最新版本。之后,这个错误就消失了。


已测试并可在Debian上使用G++ 4.9。尽管用户需要Windows解决方案。 - Rahim Khoja

1
编译器并未受到重视。在Windows上,最好的选择可能是使用Visual Studio,因为它始终保持最新状态。问题在于宏定义本身就是错误的。问题始于iomanip.h和iosbase。所以他们必须更改所有的代码。虽然有用户制作的补丁程序,但我不会完全信任它们,因为它们可能比原始副本还包含更多的错误。但这取决于你,我只使用Visual Studio Express版。

0

对于我来说,stoi仅在mingw64上正常工作。 如果你使用Codeblocks,请不要忘记检查你的项目编译器是否设置为mingw64。


0

嗯,我正在使用-std=c++98,而不是-std=c++11,但我用以下方法解决了它:

int i = std::atoi(input.c_str());

atoi() 等待 C 类型的以 null 结尾的字符串,而 c_str() 则将其转换为以 null 结尾的 char*。为了使用 atoi,我还添加了以下库:

#include <cstdlib>

我的系统是:

Ubuntu 22.04.1 LTS

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接