• std::stoi

    From Frank Tetzel@21:1/5 to All on Thu Nov 5 07:23:08 2015
    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


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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Francis Glassborow@21:1/5 to All on Thu Nov 5 12:25:21 2015
    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


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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From =?ISO-8859-1?Q?Elias_Salom=E3o_Helo@21:1/5 to All on Thu Nov 5 07:25:08 2015
    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.


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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From kiran@21:1/5 to All on Thu Nov 5 15:36:07 2015
    - 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! ]

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)