to_string is not a member of std, says g++ (mingw)
Asked Answered
D

14

273

I am making a small vocabulary remembering program where words would would be flashed at me randomly for meanings. I want to use standard C++ library as Bjarne Stroustroup tells us, but I have encountered a seemingly strange problem right out of the gate.

I want to change a long integer into std::string so as to be able to store it in a file. I have employed to_string() for the same. The problem is, when I compile it with g++ (version 4.7.0 as mentioned in its --‍version flag), it says:

PS C:\Users\Anurag\SkyDrive\College\Programs> g++ -std=c++0x ttd.cpp
ttd.cpp: In function 'int main()':
ttd.cpp:11:2: error: 'to_string' is not a member of 'std'

My program that gives this error is:

#include <string>

int main()
{
    std::to_string(0);
    return 0;
}

But, I know it can't be because msdn library clearly says it exists and an earlier question on Stack Overflow (for g++ version 4.5) says that it can be turned on with the -std=c++0x flag. What am I doing wrong?

Deannedeans answered 19/10, 2012 at 13:29 Comment(6)
Works with my GCC 4.4.5 (i.e., gives an "ambiguous overload" error); maybe your libstdc++ is outdated?Antipathetic
It works on Ideone, which uses GCC 4.5.1.Peregrination
This works for me with g++ 4.6.3. Your error refers to line 11. You say your program has "essentially been reduced to" your code, but does the code you posted also give that error?Concrete
@VaughnCato - Yep this is the exact program. I should edit it to make it clearer. Plus, what is that stray int i doing there?Deannedeans
This also doesn't work for me, even though I'm using GCC 5.3.1. However, what fixes it is simply using a later C++ standard - i.e. by compiling with the flag -std=c++11 or higher.Unlive
Similar question, maybe should be mergedDisqualify
I
221

This is a known bug under MinGW. Relevant Bugzilla. In the comments section you can get a patch to make it work with MinGW.

This issue has been fixed in MinGW-w64 distros higher than GCC 4.8.0 provided by the MinGW-w64 project. Despite the name, the project provides toolchains for 32-bit along with 64-bit. The Nuwen MinGW distro also solves this issue.

Intuit answered 19/10, 2012 at 13:43 Comment(12)
Zing. That's a nice nugget to ace an answer with. +1Darton
Further discussion here: #8542721Volta
I'm using MinGW-w64 version 4.8.1 and it is not working. I copied the exact program from the question. I still get the 'to_string' is not a member of 'std' error. I compiled it as: g++ -std=c++0x -o tostring_test.exe tostring_test.cppBrokaw
@Brokaw I can't explain why, but I got std::to_string to work by installing MinGW 4.8.1 (specifically, x32-4.8.1-posix-dwarf-rev5) through MinGW-builds. In contrast, to_string did not work with MinGW 4.8.1 installed through either the "mingw-get" installer or the TDM-GCC package.Standstill
The patch wasn't enough for me, I had to #define _GLIBCXX_USE_C99 1 in C:\MinGW\lib\gcc\mingw32\4.8.1\include\c++\mingw32\bits\c++config.h as described hereLoculus
I think the nuwen distro just copies the MinGW-w64 files, so they're the same fix.Rhu
The one day there's finally convenient standardized way to convert numbers to strings (after all the years of jerking with itoa), someone just has to ruin it.Justify
I am getting this same problem with the distribution of gnu-libstdc++ provided by the Microsoft Visual Studio cross platform native tools. It claims to be 4.9. I tried setting _GLIBCXX_USE_C99 to 1 but that introduced compiler errors within the stl itself. (using Clang 3.6)Foeticide
@Standstill "MinGW-builds" are builds of MinGW-w64, a different project to MinGW. One reason that MinGW-w64 was forked off was so they they could in fact fix bugs like the one this thread was about.Disqualify
I can confirm that the bug was still present in MinGW 4.9.2 and it was fixed in currently latest 4.9.3 release. Meanwhile, MinGW-w64 already offers GCC 5.3.0.Telegraphic
My compiler suports C++11 and it defenetly compiles with the flag -std=c++11, but geting exact the same error mesage.Roughcast
Still actual in 2023 on Debian riscv64.Becnel
P
127
#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' ;
}

do not forget to include #include <sstream>

Parasiticide answered 31/12, 2013 at 18:23 Comment(2)
Is there a way to include stod as well?Giveandtake
Please note that this is not an exact solution in regard to trailing zeros. The original std::to_string(0.75) will usually return "0.750000" (the default sprintf %d format) while this will return "0.75". See here for reason: https://mcmap.net/q/46972/-c-11-std-to_string-double-no-trailing-zerosAppellant
U
49

As suggested this may be an issue with your compiler version.

Try using the following code to convert a long to std::string:

#include <sstream>
#include <string>
#include <iostream>

int main() {
    std::ostringstream ss;
    long num = 123456;
    ss << num;
    std::cout << ss.str() << std::endl;
}
Ustulation answered 19/10, 2012 at 14:3 Comment(3)
I haven't run it but thank you for solving my immediate dilemma. Now I only have to figure out how it works. :)Deannedeans
Just a note that if you want an std::string, you want to use .str() instead.Incompletion
Please note that this is not an exact solution in regard to trailing zeros. The original std::to_string(0.75) will usually return "0.750000" (the default sprintf %d format) while this will return "0.75". See here for reason: https://mcmap.net/q/46972/-c-11-std-to_string-double-no-trailing-zerosAppellant
O
20

Use this function...

    #include<sstream>
    template <typename T>
    std::string to_string(T value)
    {
      //create an output string stream
      std::ostringstream os ;

      //throw the value into the string stream
      os << value ;

      //convert the string stream into a string and return
      return os.str() ;
    }

    //you can also do this
    //std::string output;
    //os >> output;  //throw whats in the string stream into the string
Opportuna answered 3/6, 2014 at 6:28 Comment(4)
Please, add some explanation (comments at least) in your answers, no matter how easy the code might be to understand.Bonine
It's good to have a workaround, but I think its usually best to figure out why the std solution isn't compiling rather than hack something togetherShaia
The "standard" way (std::to_string) often works -- except when it doesn't. sstream is the "standard" workaround.Lipfert
Please note that this is not an exact solution in regard to trailing zeros. The original std::to_string(0.75) will usually return "0.750000" (the default sprintf %d format) while this will return "0.75". See here for reason: https://mcmap.net/q/46972/-c-11-std-to_string-double-no-trailing-zerosAppellant
P
12

to_string is a current issue with Cygwin

Here's a new-ish answer to an old thread. A new one did come up but was quickly quashed, Cygwin: g++ 5.2: ‘to_string’ is not a member of ‘std’.

Too bad, maybe we would have gotten an updated answer. According to @Alex, Cygwin g++ 5.2 is still not working as of November 3, 2015.

On January 16, 2015 Corinna Vinschen, a Cygwin maintainer at Red Hat said the problem was a shortcoming of newlib. It doesn't support most long double functions and is therefore not C99 aware.

Red Hat is,

... still hoping to get the "long double" functionality into newlib at one point.

On October 25, 2015 Corrine also said,

It would still be nice if somebody with a bit of math knowledge would contribute the missing long double functions to newlib.

So there we have it. Maybe one of us who has the knowledge, and the time, can contribute and be the hero.

Newlib is here.

Pickup answered 26/11, 2015 at 4:49 Comment(1)
Seeing how they migrated from sending patches via mail to git only this year, it feels discouraging...Garonne
P
9

Change default C++ standard

From (COMPILE FILE FAILED) error: 'to_string' is not a member of 'std'

-std=c++98

To (COMPILE FILE SUCCESSFUL)

-std=c++11 or -std=c++14

Tested on Cygwin G++(GCC) 5.4.0

Petrochemical answered 10/6, 2017 at 0:21 Comment(0)
D
3

If we use a template-light-solution (as shown above) like the following:

namespace std {
    template<typename T>
    std::string to_string(const T &n) {
        std::ostringstream s;
        s << n;
        return s.str();
    }
}

Unfortunately, we will have problems in some cases. For example, for static const members:

hpp

class A
{
public:

    static const std::size_t x = 10;

    A();
};

cpp

A::A()
{
    std::cout << std::to_string(x);
}

And linking:

CMakeFiles/untitled2.dir/a.cpp.o:a.cpp:(.rdata$.refptr._ZN1A1xE[.refptr._ZN1A1xE]+0x0): undefined reference to `A::x'
collect2: error: ld returned 1 exit status

Here is one way to solve the problem (add to the type size_t):

namespace std {
    std::string to_string(size_t n) {
        std::ostringstream s;
        s << n;
        return s.str();
    }
}

HTH.

Debauchee answered 17/2, 2016 at 0:13 Comment(0)
B
3

to_string() is only present in c++11 so if c++ version is less use some alternate methods such as sprintf or ostringstream

Breathe answered 29/10, 2018 at 8:20 Comment(0)
B
2

The fact is that libstdc++ actually supported std::to_string in *-w64-mingw32 targets since 4.8.0. However, this does not include support for MinGW.org, Cygwin and variants (e.g. *-pc-msys from MSYS2). See also https://cygwin.com/ml/cygwin/2015-01/msg00245.html.

I have implemented a workaround before the bug resolved for MinGW-w64. Being different to code in other answers, this is a mimic to libstdc++ (as possible). It does not require string stream construction but depends on libstdc++ extensions. Even now I am using mingw-w64 targets on Windows, it still works well for multiple other targets (as long as long double functions not being used).

Brelje answered 31/12, 2015 at 13:54 Comment(0)
M
2

For anyone wondering why this happens on Android, it's probably because you're using a wrong c++ standard library. Try changing the c++ library in your build.gradle from gnustl_static to c++_static and the c++ standard in your CMakeLists.txt from -std=gnu++11 to -std=c++11

Manana answered 18/10, 2016 at 23:4 Comment(0)
H
1

in codeblocks go to setting -> compiler setting -> compiler flag -> select std c++11 done. I had the same problem ... now it's working !

Hesler answered 12/12, 2018 at 20:48 Comment(0)
R
1

For me, ensuring that I had:

#include <iostream>  
#include<string>  
using namespace std;

in my file made something like to_string(12345) work.

Rightful answered 19/7, 2020 at 4:48 Comment(0)
P
0

The following function solved my problem. Source:https://gist.github.com/mfathirirhas/6ba63fd59cc08b5b2583d52f17a20257

std::string to_string(int i)
{
  std::stringstream ss;
  ss << i;
  return ss.str();
} 
Paquin answered 10/5, 2023 at 3:40 Comment(0)
A
-3

This happened to me as well, I just wrote up a quick function rather than worrying about updating my compiler.

string to_string(int number){
    string number_string = "";
    char ones_char;
    int ones = 0;
    while(true){
        ones = number % 10;
        switch(ones){
            case 0: ones_char = '0'; break;
            case 1: ones_char = '1'; break;
            case 2: ones_char = '2'; break;
            case 3: ones_char = '3'; break;
            case 4: ones_char = '4'; break;
            case 5: ones_char = '5'; break;
            case 6: ones_char = '6'; break;
            case 7: ones_char = '7'; break;
            case 8: ones_char = '8'; break;
            case 9: ones_char = '9'; break;
            default : ErrorHandling("Trouble converting number to string.");
        }
        number -= ones;
        number_string = ones_char + number_string;
        if(number == 0){
            break;
        }
        number = number/10;
    }
    return number_string;
}
Anderegg answered 18/4, 2013 at 12:54 Comment(3)
This is not a substitute for std::to_string and definitely not an answer to the question.Tumefaction
Don't forget to implement std::cout too! Much better to just re-implement with lots of additional bugs than figure out the root issue!Intermediary
you're making things overcomplex. Just ones_char = '0' + number % 10;Dainedainty

© 2022 - 2024 — McMap. All rights reserved.