0

I have been able to find quite a few questions on this, but all seem to point that it should be working with gcc 4.8.1 if you compile with -std=c++11

Here is my output of g++ --version:

g++ (GCC) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Yet when I try to compile something with std::tostring even with a simple command like this:

g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -o "Main.o" "Main.cpp"

I get the following error:

Main.cpp: In function 'int main()':
Main.cpp:6:26: error: 'to_string' is not a member of 'std'
  std::string intString = std::to_string(1335);

Even on a file as simple as:

#include <string>
#include <cstdio>

int main()
{
    std::string intString = std::to_string(1335);
    printf(intString.c_str());
    return 0;
}

Any ideas? This is on Windows 7 64-bit using the latest version of MinGW.

pmr
  • 58,701
  • 10
  • 113
  • 156
Nabren
  • 582
  • 1
  • 7
  • 17
  • 1
    Pls provide the code where it shows the error. – vaibhav kumar Mar 22 '14 at 00:34
  • You should at least show us the definition of `testFunction` – Anthony Kong Mar 22 '14 at 00:35
  • Did you include the header like #include ? Did you use "std::" or "using namespace std"? – shawn1874 Mar 22 '14 at 00:36
  • I get this error even on something as simple as: int main() { printf(std::to_string(1335)); return 0; } – Nabren Mar 22 '14 at 00:36
  • @Nabren, unless you include the header where it is declared/defined, this isn't allowed in C++. – vonbrand Mar 22 '14 at 00:38
  • 1
    I updated the code to be a more simple file that still produces the error. I know you have to include the header that defines it... – Nabren Mar 22 '14 at 00:41
  • Printf doesn't take a std::string argument. – stefan Mar 22 '14 at 00:43
  • @stefan, std::string is convertable into a const char*, though. Regardless, that's not even the issue... just a bad example on my part. I'll update the code again so that isn't made to be the blame. – Nabren Mar 22 '14 at 00:46
  • @Nabren: yes, but the conversion isn't implicit. You have to call .c_str() if something is irrelevant to your question, don't even include it.. – stefan Mar 22 '14 at 00:47
  • 2
    you'r useing cygwin?MinGW? – jfly Mar 22 '14 at 00:48
  • MinGW, but all indications seem to be pointing to the latest version of MinGW has this working. – Nabren Mar 22 '14 at 00:49
  • 3
    @Nabren Here's the [bug report](http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52015) tracking this issue. From reading that, it's fixed if you use mingw-w64, still broken for the 32-bit version. – Praetorian Mar 22 '14 at 00:54
  • Further discussion here: http://stackoverflow.com/questions/8542221/stdstoi-doesnt-exist-in-g-4-6-1-on-mingw – jfly Mar 22 '14 at 00:57
  • @Praetorian, looks like that was it. I was skimming over some of those bugs reports and kept seeing indication it was fixed in 4.8 - I guess I didn't read down far enough :( – Nabren Mar 22 '14 at 00:59
  • @stefan Are you the real STL? :) – David G Mar 22 '14 at 01:27
  • @0x499602D2 Afraid not, my name is SDL though ;-) – stefan Mar 22 '14 at 10:27

1 Answers1

3

MinGW is a port of GCC over to windows. Your issue is a MinGW issue (as some comments have already noticed)
If you're going to be doing REAL C++ development on windows, I suggest you use MSVC. No, really, despite microsoft being evil.
Anyway, here is Coliru showing it works fine on legitimate gcc: away!.
And this is how I would do it (if you're using c++11, might as well use actual STL?)

(Note: If you want it cross platform, you can use clang or proper build tools)