简体   繁体   English

c ++ 11 to_string使用代码:: blocks -std = c ++ 11标志已被选中

[英]c++11 to_string to working with code::blocks -std=c++11 flag already selected

this is the code i am trying to compile, got it from another forum somewhere. 这是我试图编译的代码,从某个地方的另一个论坛得到它。

// 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;

}

I am getting the error: 'to_string' is not a member of 'std' 我收到错误:'to_string'不是'std'的成员

I have read in other forums to select the flags "Have g++ follow the c++11 ISO language standard [-std=c++11]" and i have and it still doesn't work. 我已经在其他论坛中阅读选择标志“让g ++遵循c ++ 11 ISO语言标准[-std = c ++ 11]”并且我拥有它仍然无效。

Any help would be greatly appreciated 任何帮助将不胜感激

I am using the GNU GCC Compiler and code::Blocks 12.11 我正在使用GNU GCC编译器和代码:: Blocks 12.11

MinGW-w64 added support for the necessary functionality since GCC 4.8, so make sure you are using at least version 4.8 GCC from MinGW-w64. 自GCC 4.8以来,MinGW-w64增加了对必要功能的支持,因此请确保至少使用MinGW-w64版本的4.8 GCC。

You can get one from here , although Code::Blocks should come with a TDM GCC toolchain which should work if it's the latest (because it's GCC 4.8.1 at the time of writing). 你可以从这里得到一个,虽然Code :: Blocks应该带有一个TDM GCC工具链,如果它是最新的,它应该可以工作(因为它在撰写本文时是GCC 4.8.1)。

i also encounter this error in codeblocks-13.12mingw-setup-TDM-GCC-481.exe(built on 27 Dec 2013). 我也在codeblocks-13.12mingw-setup-TDM-GCC-481.exe(建于2013年12月27日)中遇到此错误。 it seems a bug with tdmgcc4.8.1. 这似乎是tdmgcc4.8.1的一个错误。 maybe the newest tdmgcc whill fixed it. 也许是最新的tdmgcc whill修复它。 https://stackoverflow.com/questions/21626866/c-elipse-cdt-getting-to-string-was-not-declared-in-this-scope-with-tdm-gcc-6 the reason of above list should be the same as ours. 上面列出的原因应该是https://stackoverflow.com/questions/21626866/c-elipse-cdt-getting-to-string-was-not-declared-in-this-scope-with-tdm-gcc-6和我们一样。

============================== The std::to_string functions are guarded by !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF). ============================== std :: to_string函数由!defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF)保护。 MinGW defines this symbol as 1 in its os_defines.h, so you know, we cannot use it in mingw. MinGW在os_defines.h中将此符号定义为1,因此您知道,我们不能在mingw中使用它。

Easiest way to deal with this error is: 处理此错误的最简单方法是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM