使用 -std=c++11 后未在此范围内声明 'stoi'

'stoi' was not declared in this scope after using -std=c++11

很可能这很奇怪,但是当我收到 stoi 未在此范围内声明的错误时 ,我笑了,因为我熟悉此错误及其解决方案。

我勾选了这个选项 让 g++ 遵循 c++11 ISO c++ 语言标准 [-std=c++11]在代码块(16.01,使用 MinGW)的编译器设置中并尝试重新编译它,但令人惊讶的是它没有工作并且同样的错误仍然存​​在。我尝试重新安装 CodeBlocks,但没有用。

此外,我尝试使用 windows 电源 shell 和命令提示符 g++ math_handler.cpp -std=c++11,但得到了同样的错误。

我做错了什么?

代码在这里:

#include<string>

using namespace std;

int main()
{
    string body="456";
    int i=stoi(body);
}

注意

  1. 我也尝试过 -std=c++0x 和 g++。
  2. to_string() 功能相同的问题。
  3. gcc 版本 4.9.2 (tdm -1)

您是否包含了所需的头文件? #include <string>

stoi 也在 std 命名空间中,所以:

std::stoi() 

或:

using namespace std;

好的,我发现这是与 CodeBlocks 捆绑在一起的 MinGW 中的一个已知错误。我找到了解决方案 here

  1. Download mingw-to-string-gcc47.zip which contains three patched header files. (Original patches: wchar.h, stdio.h, os_defines.h)

  2. Copy wchar.h and stdio.h from the include directory in the zip file to the following directory (overwrite): C:\mingw\include (replace C:\mingw\ with the appropriate directory)

  3. Copy os_defines.h to the following directory (overwrite): C:\mingw\lib\gcc\mingw32.7.0\include\c++\mingw32\bits (replace C:\mingw\ with the appropriate directory) (replace 4.7.0 with the correct version number)