Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

std::stoi

318 views
Skip to first unread message

kiran

unread,
Nov 4, 2015, 8:00:50 AM11/4/15
to

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! ]

Frank Tetzel

unread,
Nov 5, 2015, 8:30:18 AM11/5/15
to

> 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?

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

James Kuyper

unread,
Nov 5, 2015, 8:30:18 AM11/5/15
to

On 11/04/2015 07:56 AM, kiran wrote:
>
> 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?

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

Öö Tiib

unread,
Nov 5, 2015, 8:30:18 AM11/5/15
to

On Wednesday, 4 November 2015 15:00:50 UTC+2, kiran wrote:
> 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);

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.

>
> 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?

Do you cave -std=gnu++0x or -std=c++0x option in command line?
'std::stoi' was added by C++11.

Louis Krupp

unread,
Nov 5, 2015, 8:30:18 AM11/5/15
to

On Wed, 4 Nov 2015 06:56:28 CST, kiran
<kiran.t...@googlemail.com> wrote:

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

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

Elias Salomão Helou Neto

unread,
Nov 5, 2015, 8:30:25 AM11/5/15
to

Em quarta-feira, 4 de novembro de 2015 11:00:50 UTC-2, kiran escreveu:
> 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?
>

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.

Francis Glassborow

unread,
Nov 5, 2015, 1:30:21 PM11/5/15
to

On 05/11/2015 13:25, Elias Salomão Helou Neto wrote:
>
> Em quarta-feira, 4 de novembro de 2015 11:00:50 UTC-2, kiran escreveu:
>> 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?
>>
>
> 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.
>
>
:) 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

kiran

unread,
Nov 5, 2015, 4:40:22 PM11/5/15
to

- 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
0 new messages