【问题标题】:c++11 to_string to working with code::blocks -std=c++11 flag already selectedc++11 to_string 使用 code::blocks -std=c++11 标志已经选择
【发布时间】:2013-11-22 12:30:10
【问题描述】:

这是我正在尝试编译的代码,从某个地方的另一个论坛获得。

// to_string example

#include <iostream>   // std::cout

#include <string>     // std::string, std::to_string


int main ()

{

  std::string pi = "pi is " + std::to_string(3.1415926);

  std::string perfect = std::to_string(1+2+4+7+14) + " is a perfect number";

  std::cout << pi << '\n';

  std::cout << perfect << '\n';

  return 0;

}

我收到错误: “to_string”不是“std”的成员

我已在其他论坛上阅读以选择标志“让 g++ 遵循 c++11 ISO 语言标准 [-std=c++11]”,但我有,但它仍然无法正常工作。

任何帮助将不胜感激

我正在使用 GNU GCC 编译器 和代码::Blocks 12.11

【问题讨论】:

标签: c++ gcc c++11 stl


【解决方案1】:

自 GCC 4.8 以来,MinGW-w64 添加了对必要功能的支持,因此请确保您至少使用来自 MinGW-w64 的 4.8 版 GCC。

您可以从 here 获得一个,尽管 Code::Blocks 应该附带一个 TDM GCC 工具链,如果它是最新的应该可以工作(因为在撰写本文时它是 GCC 4.8.1)。

【讨论】:

    【解决方案2】:

    我在 codeblocks-13.12mingw-setup-TDM-GCC-481.exe(建于 2013 年 12 月 27 日)中也遇到了这个错误。 tdmgcc4.8.1 似乎是一个错误。也许最新的 tdmgcc 会修复它。 https://stackoverflow.com/questions/21626866/c-elipse-cdt-getting-to-string-was-not-declared-in-this-scope-with-tdm-gcc-6 上面列出的原因应该和我们的一样。

    =============================== std::to_string 函数由 !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF) 保护。 MinGW 在其 os_defines.h 中将这个符号定义为 1,所以你知道,我们不能在 mingw 中使用它。

    【讨论】:

      【解决方案3】:

      处理此错误的最简单方法是:

      double piValue = 3.1415;
      char piBuffer[8];
      sprintf(piBuffer, "%f", piValue);
      string pi(piBuffer);
      

      【讨论】:

        猜你喜欢
        • 2014-01-10
        • 2015-09-19
        • 2012-11-21
        • 1970-01-01
        • 1970-01-01
        • 2014-07-12
        • 2016-07-08
        • 2013-09-27
        • 2015-08-16
        相关资源
        最近更新 更多