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.
我正在做一个小的词汇记忆程序,在这个程序中,单词将会随机地在我脑海中闪现,以获得意义。我想使用标准c++库,正如Bjarne Stroustroup告诉我们的,但我遇到了一个看起来很奇怪的问题。
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:
我想将一个长整数更改为std::string,以便能够将它存储在一个文件中。我也使用了to_string()。问题是,当我用g++编译(版本4.7.0如上所述,版本标志),它说:
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?
但是,我知道这是不可能的,因为msdn库明确地说它存在,而先前关于Stack Overflow的一个问题(对于g++版本4.5)说它可以用-std=c++0x标志打开。我做错了什么?
10 个解决方案
#1
196
This is a known bug under MinGW. Relevant Bugzilla. In the comments section you can get a patch to make it work with MinGW.
这是已知的MinGW中的错误。Bugzilla相关。在注释部分,您可以得到一个补丁,使它与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.
这一问题已经在MinGW-w64项目中被固定在比GCC 4.8.0更高的版本中。尽管名称如此,该项目仍然为32位和64位提供了工具链。Nuwen MinGW发行版也解决了这个问题。
#2
104
#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>
不要忘记包含#include
#3
44
As suggested this may be an issue with your compiler version.
正如所暗示的,这可能是编译器版本的问题。
Try using the following code to convert a long
to std::string
:
尝试使用以下代码将long转换为std::string:
#include <sstream>
#include <string>
#include <iostream>
int main() {
std::ostringstream ss;
long num = 123456;
ss << num;
std::cout << ss.str() << std::endl;
}
#4
18
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
#5
10
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’.
这是对一个老问题的新回答。一个新的出现了,但是很快就被取消了,Cygwin: g+ 5.2: ' to_string '不是' 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.
太糟糕了,也许我们会得到一个更新的答案。据@Alex表示,Cygwin g+ 5.2截至2015年11月3日仍在工作。
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.
2015年1月16日,红帽的Cygwin维护者Corinna Vinschen说这个问题是newlib的缺点。它不支持长双函数,因此不支持C99。
Red Hat is,
红色的帽子,
... still hoping to get the "long double" functionality into newlib at one point.
…仍然希望将“long double”功能放到newlib中。
On October 25, 2015 Corrine also said,
2015年10月25日,Corrine也说,
It would still be nice if somebody with a bit of math knowledge would contribute the missing long double functions to newlib.
如果有一点数学知识的人会把丢失的长双函数贡献给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.
Newlib在这里。
#6
4
Change default C++ standard
改变默认的c++标准
From (COMPILE FILE FAILED) error: 'to_string' is not a member of 'std'
从(编译文件失败)错误:'to_string'不是'std'的成员
-std=c++98
化c + + 98
To (COMPILE FILE SUCCESSFUL)
(编译文件成功)
-std=c++11 or -std=c++14
化c++ 11或化c++ 14
Tested on Cygwin G++(GCC) 5.4.0
在Cygwin gwin g++ (GCC) 5.4.0上测试
#7
2
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.
事实上,libstdc++实际上支持*-w64-mingw32中的std::to_string,从4.8.0开始。但是,这并不包括对MinGW.org、Cygwin和变体(例如,MSYS2的*-pc-msys)的支持。参见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).
在解决MinGW-w64的bug之前,我实现了一个解决方案。与其他答案中的代码不同,这是对libstdc++ +的模仿(尽可能多地)。它不需要字符串流结构,但依赖于libstdc+扩展。即使现在我在Windows上使用mingw-w64目标,它仍然适用于多个其他目标(只要不使用长双函数)。
#8
2
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
对于任何想知道为什么在Android上发生这种情况的人来说,这可能是因为您使用了错误的c++标准库。尝试在构建中更改c++库。从gnustl_static到c++_static,以及CMakeLists中的c++标准。txt从-std=gnu+ 11到-std=c++11
#9
1
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:
不幸的是,我们有时会遇到问题。例如,对于静态const成员:
hpp
高压泵
class A
{
public:
static const std::size_t x = 10;
A();
};
cpp
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):
这里有一种解决问题的方法(添加到size_t类型):
namespace std {
std::string to_string(size_t n) {
std::ostringstream s;
s << n;
return s.str();
}
}
HTH.
HTH。
#10
-5
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;
}
#1
196
This is a known bug under MinGW. Relevant Bugzilla. In the comments section you can get a patch to make it work with MinGW.
这是已知的MinGW中的错误。Bugzilla相关。在注释部分,您可以得到一个补丁,使它与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.
这一问题已经在MinGW-w64项目中被固定在比GCC 4.8.0更高的版本中。尽管名称如此,该项目仍然为32位和64位提供了工具链。Nuwen MinGW发行版也解决了这个问题。
#2
104
#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>
不要忘记包含#include
#3
44
As suggested this may be an issue with your compiler version.
正如所暗示的,这可能是编译器版本的问题。
Try using the following code to convert a long
to std::string
:
尝试使用以下代码将long转换为std::string:
#include <sstream>
#include <string>
#include <iostream>
int main() {
std::ostringstream ss;
long num = 123456;
ss << num;
std::cout << ss.str() << std::endl;
}
#4
18
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
#5
10
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’.
这是对一个老问题的新回答。一个新的出现了,但是很快就被取消了,Cygwin: g+ 5.2: ' to_string '不是' 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.
太糟糕了,也许我们会得到一个更新的答案。据@Alex表示,Cygwin g+ 5.2截至2015年11月3日仍在工作。
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.
2015年1月16日,红帽的Cygwin维护者Corinna Vinschen说这个问题是newlib的缺点。它不支持长双函数,因此不支持C99。
Red Hat is,
红色的帽子,
... still hoping to get the "long double" functionality into newlib at one point.
…仍然希望将“long double”功能放到newlib中。
On October 25, 2015 Corrine also said,
2015年10月25日,Corrine也说,
It would still be nice if somebody with a bit of math knowledge would contribute the missing long double functions to newlib.
如果有一点数学知识的人会把丢失的长双函数贡献给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.
Newlib在这里。
#6
4
Change default C++ standard
改变默认的c++标准
From (COMPILE FILE FAILED) error: 'to_string' is not a member of 'std'
从(编译文件失败)错误:'to_string'不是'std'的成员
-std=c++98
化c + + 98
To (COMPILE FILE SUCCESSFUL)
(编译文件成功)
-std=c++11 or -std=c++14
化c++ 11或化c++ 14
Tested on Cygwin G++(GCC) 5.4.0
在Cygwin gwin g++ (GCC) 5.4.0上测试
#7
2
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.
事实上,libstdc++实际上支持*-w64-mingw32中的std::to_string,从4.8.0开始。但是,这并不包括对MinGW.org、Cygwin和变体(例如,MSYS2的*-pc-msys)的支持。参见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).
在解决MinGW-w64的bug之前,我实现了一个解决方案。与其他答案中的代码不同,这是对libstdc++ +的模仿(尽可能多地)。它不需要字符串流结构,但依赖于libstdc+扩展。即使现在我在Windows上使用mingw-w64目标,它仍然适用于多个其他目标(只要不使用长双函数)。
#8
2
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
对于任何想知道为什么在Android上发生这种情况的人来说,这可能是因为您使用了错误的c++标准库。尝试在构建中更改c++库。从gnustl_static到c++_static,以及CMakeLists中的c++标准。txt从-std=gnu+ 11到-std=c++11
#9
1
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:
不幸的是,我们有时会遇到问题。例如,对于静态const成员:
hpp
高压泵
class A
{
public:
static const std::size_t x = 10;
A();
};
cpp
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):
这里有一种解决问题的方法(添加到size_t类型):
namespace std {
std::string to_string(size_t n) {
std::ostringstream s;
s << n;
return s.str();
}
}
HTH.
HTH。
#10
-5
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;
}