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

C++C++11G++MingwTostring

C++ Problem Overview


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 tells us, but I have encountered a seemingly strange problem right out of the gate.

I want to change a long integer into std::string so as to be able to store it in a file. I have employed to_string() for the same. The problem is, when I compile it with g++ (version 4.7.0 as mentioned in its --‍version flag), it says:

PS C:\Users\Anurag\SkyDrive\College\Programs> g++ -std=c++0x ttd.cpp
ttd.cpp: In function 'int main()':
ttd.cpp:11:2: error: 'to_string' is not a member of 'std'

My program that gives this error is:

#include <string>

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

But, I know it can't be because msdn library clearly says it exists and an earlier question on Stack Overflow (for g++ version 4.5) says that it can be turned on with the -std=c++0x flag. What am I doing wrong?

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

#include <string>
#include <sstream>

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

#include <iostream>

int main()
{
    std::cout << patch::to_string(1234) << '\n' << patch::to_string(1234.56) << '\n' ;
}

do not forget to include #include <sstream>

Solution 3 - C++

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

Solution 4 - C++

Use this function...

    #include<sstream>
    template <typename T>
    std::string to_string(T value)
    {
      //create an output string stream
      std::ostringstream os ;
      
      //throw the value into the string stream
      os << value ;

      //convert the string stream into a string and return
      return os.str() ;
    }

    //you can also do this
    //std::string output;
    //os >> output;  //throw whats in the string stream into the string

Solution 5 - C++

to_string is a current issue with Cygwin

Here's a new-ish answer to an old thread. A new one did come up but was quickly quashed, Cygwin: g++ 5.2: ‘to_string’ is not a member of ‘std’.

Too bad, maybe we would have gotten an updated answer. According to @Alex, Cygwin g++ 5.2 is still not working as of November 3, 2015.

On January 16, 2015 Corinna Vinschen, a Cygwin maintainer at Red Hat said the problem was a shortcoming of newlib. It doesn't support most long double functions and is therefore not C99 aware.

Red Hat is,

> ... still hoping to get the "long double" functionality into newlib at one point.

On October 25, 2015 Corrine also said,

> It would still be nice if somebody with a bit of math knowledge would contribute the missing long double functions to newlib.

So there we have it. Maybe one of us who has the knowledge, and the time, can contribute and be the hero.

Newlib is here.

Solution 6 - C++

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

Solution 7 - C++

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.

Solution 8 - C++

to_string() is only present in c++11 so if c++ version is less use some alternate methods such as sprintf or ostringstream

Solution 9 - C++

The fact is that libstdc++ actually supported std::to_string in *-w64-mingw32 targets since 4.8.0. However, this does not include support for MinGW.org, Cygwin and variants (e.g. *-pc-msys from MSYS2). See also https://cygwin.com/ml/cygwin/2015-01/msg00245.html.

I have implemented a workaround before the bug resolved for MinGW-w64. Being different to code in other answers, this is a mimic to libstdc++ (as possible). It does not require string stream construction but depends on libstdc++ extensions. Even now I am using mingw-w64 targets on Windows, it still works well for multiple other targets (as long as long double functions not being used).

Solution 10 - C++

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

Solution 11 - C++

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

Solution 12 - C++

For me, ensuring that I had:

#include <iostream>  
#include<string>  
using namespace std;

in my file made something like to_string(12345) work.

Solution 13 - C++

This happened to me as well, I just wrote up a quick function rather than worrying about updating my compiler.

string to_string(int number){
    string number_string = "";
    char ones_char;
    int ones = 0;
    while(true){
        ones = number % 10;
        switch(ones){
            case 0: ones_char = '0'; break;
            case 1: ones_char = '1'; break;
            case 2: ones_char = '2'; break;
            case 3: ones_char = '3'; break;
            case 4: ones_char = '4'; break;
            case 5: ones_char = '5'; break;
            case 6: ones_char = '6'; break;
            case 7: ones_char = '7'; break;
            case 8: ones_char = '8'; break;
            case 9: ones_char = '9'; break;
            default : ErrorHandling("Trouble converting number to string.");
        }
        number -= ones;
        number_string = ones_char + number_string;
        if(number == 0){
            break;
        }
        number = number/10;
    }
    return number_string;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnurag KaliaView Question on Stackoverflow
Solution 1 - C++RapptzView Answer on Stackoverflow
Solution 2 - C++edgarmtzeView Answer on Stackoverflow
Solution 3 - C++andreView Answer on Stackoverflow
Solution 4 - C++Nirav PatelView Answer on Stackoverflow
Solution 5 - C++user2603432View Answer on Stackoverflow
Solution 6 - C++JomaView Answer on Stackoverflow
Solution 7 - C++BorisView Answer on Stackoverflow
Solution 8 - C++Aishwarya MittalView Answer on Stackoverflow
Solution 9 - C++FrankHBView Answer on Stackoverflow
Solution 10 - C++kynnysmattoView Answer on Stackoverflow
Solution 11 - C++ShifaTView Answer on Stackoverflow
Solution 12 - C++David ChopinView Answer on Stackoverflow
Solution 13 - C++Jeff PopeView Answer on Stackoverflow