如何修复 C++ 错误:'stoi' is not a member of 'std'

问题

编译 C++ 代码时,你可能会遇到以下错误消息:

stoi_error.txt
/home/uli/myproject/main.cpp:162:26: error: 'stoi' is not a member of 'std'
  162 |                 g = std::stoi(argv[3])

解决方案

你需要在 C++ 代码中包含 <string> 头文件。

stoi 函数是 C++ 标准库的一部分,定义在 <string> 头文件中。

在你的 C++ 文件顶部添加此行:

fix_stoi_include.cpp
#include <string>

之后,再次尝试编译你的代码。错误应该已解决。


Check out similar posts by category: C/C++