3

I am using codeblocks and i can't make stoi() function work. I read other questions regarding this issue but I couldn't solve it. I checked C+11, I am using namespace std and I have the string header. I don't know how can I solve this problem.

error: 'stoi' was not declared in this scope
user3526673
  • 63
  • 2
  • 7

3 Answers3

1

found the answer on almost the same question here:

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

From this link you can download a .zip, than follow the instructions.

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

Attention, maybe you will haver errors with later versions, after using the copy/paste:

As Conduit said here:

"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 afai"

Community
  • 1
  • 1
0

I would use atoi on the str.data() returned value, since it is supported in all compiler versions.

George Nechifor
  • 439
  • 3
  • 12
0

Use

#include <string>

That should work. For reference: C++11 standard plus minor editorial changes.

Or perhaps you are forgetting to prefix with std::?

A workaround is calling .c_str() and using the <cstdlib> or <stdlib.h> functions.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • I am already using that. That is why i dont understand the problem. I did everything i see from other posts and still not working. – user3526673 Apr 14 '14 at 15:04
  • In that case, can you just search in the header file? If needed make an file including only `` and search in the preprocessor output. – Deduplicator Apr 14 '14 at 15:06
  • Sorry, im pretty new to these stuff so what exactly should i do? I found the string.h file but i didnt understand a thing. @Deduplicator – user3526673 Apr 14 '14 at 15:13
  • @user3526673: First, ``and `` are different header files with different content. You really want the one without extension. Probably in the same directory though. Then serch for the prototype of `stoi()`. Anyway, first check if you used the right namespace. – Deduplicator Apr 14 '14 at 15:17
  • I am looking at include files in MinGW folder and all the files are header files with the little "h" icon on them. Its name was actually "string" but i said string.h because it was a header file. There are no other string in the header folder – user3526673 Apr 14 '14 at 15:31
  • By the way, for C++11, there's [N3485](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3485.pdf), if you'd appreciate a later version. – chris Apr 14 '14 at 15:31
  • @chris: What else all is in there? – Deduplicator Apr 14 '14 at 15:35
  • @Deduplicator, No clue to be honest. 4 pages of stuff by the looks of it :p – chris Apr 14 '14 at 15:37