Trying to use C++11

Apr 3, 2015 at 12:01am
I've been trying to use the stoi (String to Integer) command, which is part of C++11, but I can't seem to get it to work. I've tried to compile in Dev-C++, Code::Blocks with C++11 on, and with the MinGW GCC/G++ (Version 4.8.1) with and without the -std=c++11/-std=c++0x lines, but every attempt has met with the compiler saying that 'stoi' has not been declared. Anyone know how to fix this or get it working?
Last edited on Apr 3, 2015 at 12:01am
Apr 3, 2015 at 12:42am
Can you post your minimal example that gives that error?
Apr 3, 2015 at 12:59am
I think it is a (continuing) bug with MinGW. Alas.
Apr 3, 2015 at 1:10am
Code:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {

string myString = "15264";

int myInt = stoi (myString);

cout << myInt;

system ("pause");
return 0;

}

Dev-C++ Result:

In function 'int main()':
'stoi' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
[Build Error] [main.o] Error 1

Code::Blocks Result:

In function 'int main()':
'stoi' was not declared in this scope
'system' was not declared in this scope
Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s))

MinGW GCC/G++ Result:

main.cpp: In function 'int main()';
main.cpp:11:31: error: 'stoi' was not declared in this scope
int myInt = stoi (myString);
main.cpp:15:20: error: 'system' was not declared in this scope
system ("pause");

Note: The whole system pause bit is just a Dev-C++ thing for pausing the program, so the fact that it throws up an error in Code::Blocks and MinGW isn't actually an issue
Last edited on Apr 3, 2015 at 1:11am
Apr 3, 2015 at 7:25am
well, can you show us stoi? Also, please always use code tags, its under the format section and looks like this <>. Mark all of your code and click it.

Edit: Whoops, thanks Peter :D
Last edited on Apr 3, 2015 at 10:08am
Apr 3, 2015 at 10:05am
TarikNeaj wrote:
can you show us stoi?

stoi is a standard function.

http://www.cplusplus.com/reference/string/stoi/
Last edited on Apr 3, 2015 at 10:06am
Apr 3, 2015 at 10:23am
MingW 4.8.1's stdlib does have std::stoi. It's no bug.
"system" is inside stdlib.h.
Make sure you have -std=c++11 in your compiler's command line. That can be the only issue (unless you been toying with it too much).

As a side note, stay away from Dev-C++.
Last edited on Apr 3, 2015 at 10:25am
Apr 3, 2015 at 11:03am
MingW 4.8.1's stdlib does have std::stoi. It's no bug.

in compiler terms, not supporting a standard feature is considered a bug. for example, when gcc 4.8 didnt support <regex> it was labeled a bug. (that might not be the correct version/header, but i think you get the jist)
Apr 3, 2015 at 1:22pm
There is a difference between "does" and "doesn't".
Apr 3, 2015 at 2:11pm
There is a difference between "does" and "doesn't".

my mistake. i read that as doesn't.
Apr 4, 2015 at 1:41am
I've indirectly fixed the problem. Instead of trying to use C++11's stoi function, I've just changed the string to a char array and used the standard atoi function instead, which seems to be working fine.
Apr 4, 2015 at 3:41am
Use .c_str() instead of evil character arrays.
Topic archived. No new replies allowed.