Anu*_*lia 241 c++ mingw g++ tostring c++11
我正在制作一个小词汇记忆程序,其中的单词将随机闪现在我的意思中.我想使用标准的C++库,就像Bjarne Stroustroup告诉我们的那样,但我遇到了一个看似奇怪的问题.
我想将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'
Run Code Online (Sandbox Code Playgroud)
我的程序给出了这个错误:
#include <string>
int main()
{
std::to_string(0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,我知道它不可能是因为msdn库清楚地说它存在并且早先关于Stack Overflow的问题(对于g ++版本4.5)说它可以用-std=c++0x
旗标打开.我究竟做错了什么?
Rap*_*ptz 211
这是MinGW下的一个已知错误.相关的Bugzilla.在评论部分,您可以获得一个补丁,使其适用于MinGW.
此问题已在MinGW-w64发行版中修复,该发行版高于MinGW-w64项目提供的GCC 4.8.0 .尽管有这个名字,该项目提供了32位和64位的工具链.该Nuwen MinGW的发行版还解决了这个问题.
cMi*_*nor 115
#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' ;
}
Run Code Online (Sandbox Code Playgroud)
别忘了包括 #include <sstream>
and*_*dre 46
如建议的那样,这可能是您的编译器版本的问题.
尝试使用以下代码将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;
}
Run Code Online (Sandbox Code Playgroud)
小智 20
使用此功能......
#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
Run Code Online (Sandbox Code Playgroud)
小智 12
这是旧线程的新答案.一个新的确实出现但很快被撤销, Cygwin:g ++ 5.2:'to_string'不是'std'的成员.
太糟糕了,也许我们会得到一个更新的答案.据@Alex称,截至2015年11月3日,Cygwin g ++ 5.2仍未运行.
2015年1月16日,Red Hat的Cygwin维护人员Corinna Vinschen 表示,这个问题是newlib的缺点.它不支持大多数长双精度函数,因此不支持C99.
红帽是,
...仍然希望在某一点上将"长双"功能带入newlib.
2015年10月25日,Corrine也说,
如果拥有一些数学知识的人会将缺少的长双重函数贡献给newlib,那仍然会很好.
因此,我们有它.也许我们中的一个拥有知识和时间的人可以贡献并成为英雄.
Newlib在这里.
更改默认C++标准
从(COMPILE FILE FAILED)错误:'to_string'不是'std'的成员
-std = C++ 98
到(编译文件成功)
-std = c ++ 11或-std = c ++ 14
在Cygwin G ++(GCC)5.4.0上测试
归档时间: |
|
查看次数: |
349878 次 |
最近记录: |