using to_string function in mingw gcc 4.7.1

I wanted to use the to_string function in my program built with gcc 4.7.1. But i was getting an error that it was not declared in the scope. So, I downloaded the patch from the link: http://tehsausage.com/mingw-to-string and followed all the instructions in that link.

But how do I access the to_string function after doing this?
Not sure why a patch is needed. Have you enabled C++11 features by passing the flag -std=c++11 and included the <string> header?
> I wanted to use the to_string function in my program built with gcc 4.7.1

Till MinGW GCC fixes the issue, you can do something this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#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' ;
}
Hey JLBorges,

I tried using your code in my program, but I got the following error messages.

E:\programs\_Projects\Hexcon\generator\main.cpp|8|error: invalid use of incomplete type 'std::ostringstream {aka class std::basic_ostringstream<char>}'|

e:\pro_lan\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\iosfwd|103|error: declaration of 'std::ostringstream {aka class std::basic_ostringstream<char>}'|

The main.cpp is the file that i'm working on. Well, i'm using the code::blocks 12.11 IDE.

Thanks for you're solution...
Last edited on
did you #include <sstream> ?
Thanks tntxtnt. That was the mistake...
Topic archived. No new replies allowed.