to_string is not a member of std, says g++ (mingw)

前端 未结 13 1130
不思量自难忘°
不思量自难忘° 2020-11-22 05:41

I am making a small vocabulary remembering program where words would would be flashed at me randomly for meanings. I want to use standard C++ library as Bjarne Stroustroup t

相关标签:
13条回答
  • 2020-11-22 06:00

    in codeblocks go to setting -> compiler setting -> compiler flag -> select std c++11 done. I had the same problem ... now it's working !

    0 讨论(0)
  • 2020-11-22 06:01

    As suggested this may be an issue with your compiler version.

    Try using the following code to convert a long to std::string:

    #include <sstream>
    #include <string>
    #include <iostream>
    
    int main() {
        std::ostringstream ss;
        long num = 123456;
        ss << num;
        std::cout << ss.str() << std::endl;
    }
    
    0 讨论(0)
  • 2020-11-22 06:01

    Change default C++ standard

    From (COMPILE FILE FAILED) error: 'to_string' is not a member of 'std'

    -std=c++98

    To (COMPILE FILE SUCCESSFUL)

    -std=c++11 or -std=c++14

    Tested on Cygwin G++(GCC) 5.4.0

    0 讨论(0)
  • 2020-11-22 06:01

    For anyone wondering why this happens on Android, it's probably because you're using a wrong c++ standard library. Try changing the c++ library in your build.gradle from gnustl_static to c++_static and the c++ standard in your CMakeLists.txt from -std=gnu++11 to -std=c++11

    0 讨论(0)
  • 2020-11-22 06:05

    This is a known bug under MinGW. Relevant Bugzilla. In the comments section you can get a patch to make it work with MinGW.

    This issue has been fixed in MinGW-w64 distros higher than GCC 4.8.0 provided by the MinGW-w64 project. Despite the name, the project provides toolchains for 32-bit along with 64-bit. The Nuwen MinGW distro also solves this issue.

    0 讨论(0)
  • 2020-11-22 06:05

    If we use a template-light-solution (as shown above) like the following:

    namespace std {
        template<typename T>
        std::string to_string(const T &n) {
            std::ostringstream s;
            s << n;
            return s.str();
        }
    }
    

    Unfortunately, we will have problems in some cases. For example, for static const members:

    hpp

    class A
    {
    public:
    
        static const std::size_t x = 10;
    
        A();
    };
    

    cpp

    A::A()
    {
        std::cout << std::to_string(x);
    }
    

    And linking:

    CMakeFiles/untitled2.dir/a.cpp.o:a.cpp:(.rdata$.refptr._ZN1A1xE[.refptr._ZN1A1xE]+0x0): undefined reference to `A::x'
    collect2: error: ld returned 1 exit status
    

    Here is one way to solve the problem (add to the type size_t):

    namespace std {
        std::string to_string(size_t n) {
            std::ostringstream s;
            s << n;
            return s.str();
        }
    }
    

    HTH.

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