[C++] Problems with strings MinGW

  • Context: C/C++ 
  • Thread starter Thread starter utkarshakash
  • Start date Start date
  • Tags Tags
    Strings
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 6K views
utkarshakash
Gold Member
Messages
852
Reaction score
13
I'm trying to convert an integer variable to a string using to_string function of string.h header file.
Here's the part of code:

Code:
string numToStr=to_string(num);

However, when I try to compile my program I get the following error:
"error:to_string was not declared in this scope"

I'm using CodeBlocks as my IDE and my compiler is MinGW 4.8.1-4. Can anyone tell me why is this error showing up and how to fix this issue?
 
Physics news on Phys.org
jtbell said:
string.h contains functions for C-style char* "strings", not C++ strings. Try using '#include <string>' instead of '#include <string.h>'. (or in addition to it, in case you're using both C-style and C++ strings.)

If you want to use the C string functions in a C++ program, #include <cstring> is cleaner than #include <string.h>. The same naming system applies to any other C standard headers you want to use in C++.

<string.h> is "really" C and not C++, even if your C++ compiler eats it without producing any error messages.

Of course you also need #include <string> to use the C++ string functions.