g_s(mingw)说to_string不是std的成员

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的发行版还解决了这个问题.

  • 我正在使用MinGW-w64版本4.8.1,它无法正常工作.我从问题中复制了确切的程序.我仍然得到'to_string'不是'std'错误的成员.我把它编译为:g ++ -std = c ++ 0x -o tostring_test.exe tostring_test.cpp (11认同)
  • @ zam664我无法解释原因,但我通过[MinGW-builds安装MinGW 4.8.1(特别是`x32-4.8.1-posix-dwarf-rev5`)**让`std :: to_string`工作](http://sourceforge.net/projects/mingwbuilds/?source=pdlp)**.相比之下,`to_string`确实*不能与通过"mingw-get"安装程序或TDM-GCC软件包安装的MinGW 4.8.1一起使用. (5认同)
  • 补丁对我来说还不够,我不得不在`C:\ MinGW\lib\gcc\mingw32\4.8.1\include\c ++\mingw32\bits\c ++ config.h`中`#define _GLIBCXX_USE_C99 1`如[此处]所述(http://mail-index.netbsd.org/tech-toolchain/2014/04/17/msg002228.html) (5认同)
  • @Cerran"MinGW-builds"是MinGW-w64的构建版本,是与MinGW不同的项目.MinGW-W64分拆的一个原因是他们实际上可以修复像这个线程那样的bug. (3认同)
  • 这里进一步讨论:http://stackoverflow.com/questions/8542221/stdstoi-doesnt-exist-in-g-4-6-1-on-mingw (2认同)

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>

  • 请注意,这不是尾随零的精确解决方案。原始的 `std::to_string(0.75)` 通常会返回 `"0.750000"` (默认的 sprintf %d 格式),而这将返回 `"0.75"`。原因请参见此处:/sf/answers/958084011/ (2认同)

and*_*dre 46

如建议的那样,这可能是您的编译器版本的问题.

尝试使用以下代码将a转换longstd::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)

  • 有一个解决方法是好的,但我认为通常最好弄清楚为什么std解决方案不是编译而不是一起破解某些东西 (4认同)
  • 请在您的答案中添加一些解释(至少注释),无论代码有多容易理解. (2认同)

小智 12

to_string是Cygwin的当前问题

这是旧线程的新答案.一个新的确实出现但很快被撤销, 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在这里.


Jom*_*oma 7

更改默认C++标准

(COMPILE FILE FAILED)错误:'to_string'不是'std'的成员

-std = C++ 98

(编译文件成功)

-std = c ++ 11或-std = c ++ 14

在Cygwin G ++(GCC)5.4.0上测试


归档时间:

查看次数:

349878 次

最近记录:

6 年,8 月 前