“to_string” isn't a member of “std”?

后端 未结 2 766
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-02 20:08

Okay, so I have

tmp.cpp:

#include 

int main()
{
    std::to_string(0);
    return 0;
}

But when I try to compile I g

相关标签:
2条回答
  • 2020-12-02 20:23

    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.

    0 讨论(0)
  • 2020-12-02 20:26

    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.

    0 讨论(0)
提交回复
热议问题