9

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'

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.

Any help would be greatly appreciated

I am using the GNU GCC Compiler and code::Blocks 12.11

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Montana
  • 482
  • 1
  • 4
  • 16
  • 3
    possible duplicate of [to\_string is not a member of std, says so g++](http://stackoverflow.com/questions/12975341/to-string-is-not-a-member-of-std-says-so-g) – P0W Nov 10 '13 at 19:15
  • Works good there. Check your compiler version, does it support C++11? – igleyy Nov 10 '13 at 19:16
  • I think this is a compiler bug. http://ideone.com/yiIboD – haccks Nov 10 '13 at 19:18

3 Answers3

2

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.

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).

rubenvb
  • 74,642
  • 33
  • 187
  • 332
1

i also encounter this error in codeblocks-13.12mingw-setup-TDM-GCC-481.exe(built on 27 Dec 2013). it seems a bug with tdmgcc4.8.1. maybe the newest tdmgcc whill fixed it. 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.

============================== The std::to_string functions are guarded by !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.

Community
  • 1
  • 1
0

Easiest way to deal with this error is:

double piValue = 3.1415;
char piBuffer[8];
sprintf(piBuffer, "%f", piValue);
string pi(piBuffer);
V. Panchenko
  • 774
  • 1
  • 10
  • 32