to_string() issues

I have been trying to convert an integer to a string, and I ran across the function to_string(). I tried to use it, but it doesn't recognize the command. This is what I have:
#include <iostream>
#include <string>
using namespace std;
int main()
    int var = 2;
    string str = to_string(var);

What am I doing wrong?
std::to_string is a quite new addition to C++. Are you using an up to date compiler? You might have to explicitly enable C++11 mode in the compiler. If you are using GCC you can do that by passing the flag -std=c++0x.
Last edited on
You have not said what is wrong. I can only guess that either you are using an old compiler that does not support the new C++ Standard, or you are using MS VC++ 2010. If you are using MS VC++ 2010 then you can make that your program will work:

string str = to_string( ( long long )var );
Last edited on
The compiler says that to_string() is not declared, so clearly it does not recognize the command. Do I need a certain header or something?
It is in the <string> header but your compiler must be aware of/configured to use C++11. I think MSVC is enabled by default but with MinGW you need to pass "-std=c++11" to the compiler
Use a stringstream, this will work in every compiler without any special settings.
Topic archived. No new replies allowed.