8
#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.

Community
  • 1
  • 1
Games Brainiac
  • 80,178
  • 33
  • 141
  • 199

2 Answers2

15

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)
mh sattarian
  • 2,936
  • 1
  • 11
  • 13
masoud
  • 55,379
  • 16
  • 141
  • 208
  • 1
    People should note that **direct replacement of the files is not guaranteed to be safe on versions later than MinGW GCC 4.7** - use the pastebin snippets, open the files, and comment/add to the existing files. Applied as such, this still works fine on MinGW GCC 4.8.1 afaik – Conduit Sep 16 '14 at 16:25
1

Another solution is to use MinGW-w64, which works correctly out of the box. This is a fork of MinGW that can produce both 32-bit and 64-bit builds.

M.M
  • 138,810
  • 21
  • 208
  • 365