Muzib
Muzib

Reputation: 2581

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

Most probably this is weird, but when I got this error that stoi wasn't declared in this scope, I smiled because I am familiar with this error and it's solution.

I checked this option have g++ follow the c++11 ISO c++ language standard [-std=c++11] in compiler settings of Code Blocks (16.01, with MinGW) and tried recompiling it, but surprisingly it didn't work and the same error persisted. I tried re-installing CodeBlocks but that didn't work.

Also, I tried with windows power shell and command prompt with g++ math_handler.cpp -std=c++11 but got the same error.enter image description here

What am I doing wrong?

the code is here:

#include<string>

using namespace std;

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

Note:

  1. I tried with -std=c++0x and g++ too.
  2. the same problem with to_string() function.
  3. gcc version 4.9.2 (tdm -1)

Upvotes: 2

Views: 6577

Answers (2)

Muzib
Muzib

Reputation: 2581

Okay, I found that it is a known bug in MinGW bundled with CodeBlocks. I found the solution 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\4.7.0\include\c++\mingw32\bits (replace C:\mingw\ with the appropriate directory) (replace 4.7.0 with the correct version number)

Upvotes: 2

Alexander Ivanov
Alexander Ivanov

Reputation: 386

Did you include the required header file? #include <string>

stoi is also in the std namespace so:

std::stoi() 

or:

using namespace std;

Upvotes: 0

Related Questions