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.
#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>
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;
}
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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With