Games Brainiac Games Brainiac - 1 month ago 32
C++ Question

Problems with std::stoi, not working on MinGW GCC 4.7.2

#include <iostream>
#include <string>

int main()
{
std::string test = "45";
int myint = stoi(test);
std::cout << myint << '\n';
}


I tried this code on my computer which is running MinGW GCC 4.7.2. It gives me this error:

enter image description here

What am I doing wrong, I got this from cppreference. Its the exact same code. And its a different error from the one described here.

Answer

It seems your MinGW needs a patch: Enabling string conversion functions in MinGW

This patch enables the following list of C++11 functions and templates in the std namespace:

stoi, stol, stoul, stoll, stof, stod, stold, to_string, to_wstring

In above link, there is a .zip file, download it and

  • 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)
  • Copy os_defines.h to the following directory (overwrite): C:\mingw\lib\gcc\mingw32\4.7.0\include\c++\mingw32\bits (replace C:\mingw\ with the appropriate directory) (replace 4.7.0 with the correct version number)
Comments