string is not a member of std, says g++ (mingw)
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:UsersAnuragSkyDriveCollegePrograms> 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?
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' ;
}
不要忘记包含#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;
}
链接地址: http://www.djcxy.com/p/20964.html
上一篇: 将int转换为C ++字符串的最简单方法