to_string() function in codeblock not wrorking!

C++11 to_string() are not working even after the selection of -std=c++11 flag at compiler setting!

It shows a error like this~

error: ‘to_string’ was not declared in this scope…

Is there anybody know the specific reason? As i searched it through all other websites!

3 Likes

Sorry! Not satisfied with this link!! Any other ans?

Hello bansal1232, I think you have not declared the header file which has the function to_string in it i.e cstring or you can use bits/stdc++.h
See here also

Try to run yourself by including all header file relevant to this! And then answer me!

You will get the same error!

Thank you! @deepanshu_946

Try using selecting -std=c++0x flag.

Works fine with me. Either you have not included the appropriate header files or to_string is in a different scope. Try std::to_string or use the namespace std globally. Third option: ther’s something else wrong. In that case your source would be helpful.

1 Like

Which Compiler and version of gcc are you using?

to_string works for me in CodeBlocks 16.01 and std=c++0x flag set

In window or linux?

windows works fine

If you are working in windows environment. CodeBlocks 16.01 C++11 support enabled will get your job done.

If you are working in Linux environment. Try this

g++ --version

Mine is Ubuntu 16.04.1 and g++ compiler version is 5.4.0 and works fine for me

then simply type

g++ -std=c++11 prog.cpp -o prog

For older versions

g++ -std=c++0x tmp.cpp -o tmp

GCC 4.9.2 supports C++ 14 by

g++ -std=c++1y tmp.cpp -o tmp

If it does not work try using latest version of Compiler.

Hope it helps

1 Like

and if this solves your problem you can accept my answer :slight_smile:

dude is your problem sorted now

Dear @bansal1232 either std::to_string() or to_string does not work in Code::Blocks, instead of to_string you can use following code

    int i = 1212;
    stringstream ss;
    ss << i;
    string s=ss.str();

Now after using above code your integer i is converted to a string and stored in s.

3 Likes

You are absolutely right. to_string() doesn’t work on code::blocks

1 Like

Try this

Copy wchar.h and stdio.h from the include directory in the zip file to the following directory (overwrite):

C:\mingw\include

(replace C:\mingw\ with the appropriate directory)

Copy os_defines.h to the following directory (overwrite):

C:\mingw\lib\gcc\mingw32\4.7.0\include\c++\mingw32\bits

(replace C:\mingw\ with the appropriate directory)

(replace 4.7.0 with the correct version number)

2 Likes

@bansal1232 I think you lack the header which contains definition of to_string method. I would suggest you to follow my previous answer on this post and patch the corresponding headers to use to_string

//this code is execute on visual studio ide console application

int i = 1234;
string str = Convert.ToString(i);
Console.WriteLine(str);

copying wchar.h indeed helped in getting to_string working

1 Like

include bits/stdc++.h in your program.it includes almost all the libraries like string, algorithm, set, multiset etc.
Also don’t forget to write “use namespace std;