Discussion:
std::stoi
(too old to reply)
kiran
2015-11-04 12:56:28 UTC
Permalink
I have been using stoi to convert string representation of numbers into int
since long. Only recently one such program of mine stopped working giving
following error:

string_int.cpp: In function 'int main(int, char**)':
string_int.cpp:29: error: 'stoi' is not a member of 'std'

Here is the code snippet:

std::string newValStr = "777;
int newValInt = 0;
newValInt = std::stoi(newValStr);

I looked around internet and there doesn't seem to be any update on it. I am
compiling it using g++ on Redhat Linux 6.2 with following gcc version:

gcc version 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC)

Has anyone else also faced something similar?

thanks
KR
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Öö Tiib
2015-11-05 13:23:13 UTC
Permalink
Post by kiran
I have been using stoi to convert string representation of numbers into int
since long.
Only recently one such program of mine stopped working giving
string_int.cpp:29: error: 'stoi' is not a member of 'std'
std::string newValStr = "777;
int newValInt = 0;
newValInt = std::stoi(newValStr);
It is not the code. That snippet should get some other parse errors
from compiler since it clearly contains only one instance of double
quotes ("). Prefer copy-pasting full code of real program.
Post by kiran
I looked around internet and there doesn't seem to be any update on it. I am
gcc version 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC)
Has anyone else also faced something similar?
Do you cave -std=gnu++0x or -std=c++0x option in command line?
'std::stoi' was added by C++11.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
James Kuyper
2015-11-05 13:23:06 UTC
Permalink
Post by kiran
I have been using stoi to convert string representation of numbers into int
since long. Only recently one such program of mine stopped working giving
string_int.cpp:29: error: 'stoi' is not a member of 'std'
std::string newValStr = "777;
int newValInt = 0;
newValInt = std::stoi(newValStr);
I looked around internet and there doesn't seem to be any update on it. I am
gcc version 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC)
Has anyone else also faced something similar?
stoi is not present in n1836.pdf, the oldest version of the C++ standard
that I have. It is present in n2723.pdf, the second oldest (2008-08-25)
and n3797.pdf, the lastest version I have (2013-10-13).

I didn't even know it existed till today, so I can't tell you anything
about what happened with it.
--
James Kuyper


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Louis Krupp
2015-11-05 13:24:41 UTC
Permalink
On Wed, 4 Nov 2015 06:56:28 CST, kiran
Post by kiran
I have been using stoi to convert string representation of numbers into int
since long. Only recently one such program of mine stopped working giving
string_int.cpp:29: error: 'stoi' is not a member of 'std'
std::string newValStr = "777;
int newValInt = 0;
newValInt = std::stoi(newValStr);
I looked around internet and there doesn't seem to be any update on it. I am
gcc version 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC)
Has anyone else also faced something similar?
thanks
KR
A search for "g++ std::stoi" turned up this:

http://stackoverflow.com/questions/14743904/stdstoi-missing-in-g-4-7-2

I tried your code (with the missing quote added after the 777) on FC
22 with g++ 5.1.1; I saw the same problem, and I was able to make it
work with -std=c++11.

I don't know why it might have worked for you before.

Louis
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Frank Tetzel
2015-11-05 13:23:08 UTC
Permalink
Post by kiran
I have been using stoi to convert string representation of numbers
into int since long. Only recently one such program of mine stopped
string_int.cpp:29: error: 'stoi' is not a member of 'std'
std::string newValStr = "777;
int newValInt = 0;
newValInt = std::stoi(newValStr);
I looked around internet and there doesn't seem to be any update on
it. I am compiling it using g++ on Redhat Linux 6.2 with following
gcc version 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC)
Has anyone else also faced something similar?
Do you have C++11 enabled when compiling? -std=c++11? Maybe you changed
your compiler flags and it's now missing.

std::stoi was added in C++11.
http://en.cppreference.com/w/cpp/string/basic_string/stol

Regards,
Frank
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Elias Salomão Helou Neto
2015-11-05 13:25:08 UTC
Permalink
Post by kiran
I have been using stoi to convert string representation of numbers into int
since long. Only recently one such program of mine stopped working giving
string_int.cpp:29: error: 'stoi' is not a member of 'std'
std::string newValStr = "777;
int newValInt = 0;
newValInt = std::stoi(newValStr);
I looked around internet and there doesn't seem to be any update on it. I am
gcc version 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC)
Has anyone else also faced something similar?
Are you including the <string> header? End there is a missing " in the code.


Anyway, if you would like an advise, I'd recommend you to use istringstream
and operator >> in order to perform tihs conversion in a more C++ish way.

Best,
Elias.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Francis Glassborow
2015-11-05 18:25:21 UTC
Permalink
Post by Elias Salomão Helou Neto
Post by kiran
I have been using stoi to convert string representation of numbers into
int
Post by kiran
since long. Only recently one such program of mine stopped working giving
string_int.cpp:29: error: 'stoi' is not a member of 'std'
std::string newValStr = "777;
int newValInt = 0;
newValInt = std::stoi(newValStr);
I looked around internet and there doesn't seem to be any update on it. I
am
Post by kiran
gcc version 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC)
Has anyone else also faced something similar?
Are you including the <string> header? End there is a missing " in the code.
Anyway, if you would like an advise, I'd recommend you to use
istringstream
Post by Elias Salomão Helou Neto
and operator >> in order to perform tihs conversion in a more C++ish way.
Best,
Elias.
:) Surely using the C++ Standard Library is the C++ way. I think, that
as others have suggested, you need to check your compiler switches. Most
compilers allow you to select which version of C++ you are using. You
need C++ 11 or higher for stoi to be available.

Francis
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
kiran
2015-11-05 21:36:07 UTC
Permalink
- The double quotes around 777, missing in the my post above, were indeed
present in the program.
- I included the <string> header file.
- As a workaround I did use stringstream in the meantime.

The conclusion: My compiler flags were missing. Once I added -std=c++0x,
stoi is working good.

Thank you all for the responses. I am able to get it working now.

- Kiran
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Continue reading on narkive:
Loading...