"to_string" isn't a member of "std"?

95,376

Solution 1

you may want to specify the C++ version with

g++ -std=c++11 tmp.cpp -o tmp

I don't have gcc 4.8.1 at hand , but in older versions of GCC, you can use

g++ -std=c++0x tmp.cpp -o tmp

At least gcc 4.9.2 I believe also support part of C++14 by specifying

g++ -std=c++1y tmp.cpp -o tmp

Update: gcc 5.3.0 (I am using the cygwin version) supports both -std=c++14 and -std=c++17 now.

Solution 2

to_string works with the latest C++ versions like version 11. For older versions you can try using this function

#include <string>
#include <sstream>

template <typename T>
std::string ToString(T val)
{
    std::stringstream stream;
    stream << val;
    return stream.str();
}

By adding a template you can use any data type too. You have to include #include<sstream> here.

Share:
95,376

Related videos on Youtube

mueslo
Author by

mueslo

Updated on July 09, 2022

Comments

  • mueslo
    mueslo almost 2 years

    Okay, so I have

    tmp.cpp:

    #include <string>
    
    int main()
    {
        std::to_string(0);
        return 0;
    }
    

    But when I try to compile I get:

    $ g++ tmp.cpp -o tmp
    tmp.cpp: In function ‘int main()’:
    tmp.cpp:5:5: error: ‘to_string’ is not a member of ‘std’
         std::to_string(0);
         ^
    

    I'm running g++ version 4.8.1. Unlike all the other references to this error that I found out there, I am not using MinGW, I'm on Linux (3.11.2).

    Any ideas why this is happening? Is this standard behaviour and I did something wrong or is there a bug somewhere?

    • M.M
      M.M about 10 years
      With g++ -std=c++11 -o tmp tmp.cc, I get `error: 'to_string' is not a member of 'std'. g++ version is 4.8.2 . Is this a regression?
    • mueslo
      mueslo about 10 years
      Mh, not sure, but it works for me on g++ 4.9.0
    • Brian Jack
      Brian Jack over 9 years
      gcc --version using gcc tdm-2 4.8.1, compiling with -std=c++11 still yields error 'to-string' is not member of std
    • Alain1405
      Alain1405 about 8 years
      Same for me. With arm-none-eabi-gcc -std=c++11 I still get the error.
  • eluong
    eluong almost 10 years
    Had this error in Eclipse CDT on Debian 8 (Linux 3.14-2-amd64). Specifying the GCC C++ Compiler in the settings to run the command g++ -std=c++11 solved my issue! Thank you!
  • Brian Jack
    Brian Jack over 9 years
    I still get the error in gcc --version tdm-2 4.8.1 when compiling with -std=c++11
  • Max Raskin
    Max Raskin about 9 years
    I've also encountered this issue, it seems that std::to_string isn't available in gcc's standard library (libstdc++), it is however, available in libc++ which comes with LLVM/clang
  • CS Pei
    CS Pei about 9 years
    what version of gcc you are using, you may also try -std=c++1y
  • Bruno Bieri
    Bruno Bieri almost 6 years
    wouldn't it be more safe to use std::ostringstream since we only want the output of the stream?