C++ g++ (mingw) 说 to_string 不是 std 的成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12975341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
to_string is not a member of std, says g++ (mingw)
提问by Anurag Kalia
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.
我正在制作一个小词汇记忆程序,其中单词会随机闪现给我以获取含义。我想像 Bjarne Stroustroup 告诉我们的那样使用标准的 C++ 库,但我刚开始就遇到了一个看似奇怪的问题。
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:
我想将一个long
整数更改为std::string
以便能够将它存储在一个文件中。我有to_string()
同样的工作。问题是,当我用 g++(版本 4.7.0,如其 --?version 标志中提到的)编译它时,它说:
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 saysit exists and an earlier questionon 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
标志打开。我究竟做错了什么?
采纳答案by Rapptz
This is a known bug under MinGW. Relevant Bugzilla. In the comments section you can get a patchto 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 distroalso solves this issue.
此问题已在MinGW-w64 项目提供的高于 GCC 4.8.0 的 MinGW-w64 发行版中修复。尽管名称如此,但该项目提供了 32 位和 64 位的工具链。该Nuwen MinGW的发行版还解决了这个问题。
回答by cMinor
#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 <sstream>
回答by andre
As suggested this may be an issue with your compiler version.
正如所建议的,这可能是您的编译器版本的问题。
Try using the following code to convert a long
to std::string
:
尝试使用以下代码将 a 转换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;
}
回答by Nirav Patel
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
回答by Nirav Patel
to_string is a current issue with Cygwin
to_string 是 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' 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.
太糟糕了,也许我们会得到更新的答案。根据@Alex 的说法,截至 2015 年 11 月 3 日,Cygwin g++ 5.2 仍然无法运行。
On January 16, 2015 Corinna Vinschen, a Cygwin maintainer at Red Hat said the problemwas a shortcoming of newlib. It doesn't support most long double functions and is therefore not C99 aware.
2015 年 1 月 16 日,Red Hat 的 Cygwin 维护者 Corinna Vinschen表示,这个问题是 newlib 的一个缺点。它不支持大多数 long double 函数,因此不支持 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.
如果有一点数学知识的人将丢失的 long double 函数贡献给 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 来了。
回答by Joma
Change default C++ standard
更改默认 C++ 标准
From(COMPILE FILE FAILED) error: 'to_string' is not a member of 'std'
从(编译文件失败)错误:“to_string”不是“std”的成员
-std=c++98
-std=c++98
To(COMPILE FILE SUCCESSFUL)
到(编译文件成功)
-std=c++11 or -std=c++14
-std=c++11 或 -std=c++14
Tested on Cygwin G++(GCC) 5.4.0
在 Cygwin G++(GCC) 5.4.0 上测试
回答by Aishwarya Mittal
to_string() is only present in c++11 so if c++ version is less use some alternate methods such as sprintfor ostringstream
to_string() 仅存在于 c++11 中,因此如果 c++ 版本较少,请使用一些替代方法,例如sprintf或ostringstream
回答by kynnysmatto
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++ 标准库。尝试将 build.gradle 中的 c++ 库从gnustl_static
更改为c++_static
并将 CMakeLists.txt 中的 c++ 标准从-std=gnu++11
更改为-std=c++11
回答by FrankHB
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.
事实是,从 4.8.0 开始std::to_string
,*-w64-mingw32 目标实际上支持 libstdc++ 。但是,这不包括对 MinGW.org、Cygwin 和变体(例如 MSYS2 中的 *-pc-msys)的支持。另见https://cygwin.com/ml/cygwin/2015-01/msg00245.html。
I have implemented a workaroundbefore 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 的错误之前,我已经实施了一个解决方法。与其他答案中的代码不同,这是对 libstdc++ 的模仿(尽可能)。它不需要字符串流构造,但依赖于 libstdc++ 扩展。即使现在我在 Windows 上使用 mingw-w64 目标,它仍然适用于多个其他目标(只要long double
不使用函数)。
回答by Boris
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 constmembers:
不幸的是,我们在某些情况下会遇到问题。例如,对于静态常量成员:
hpp
马力
class A
{
public:
static const std::size_t x = 10;
A();
};
cpp
cp
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.
哈。