Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[C++] Problems with strings MinGW

  1. Aug 22, 2014 #1


    User Avatar
    Gold Member

    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 (Text):
    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?
  2. jcsd
  3. Aug 22, 2014 #2


    User Avatar

    Staff: Mentor

    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.)
  4. Aug 22, 2014 #3


    Staff: Mentor

  5. Aug 22, 2014 #4


    User Avatar
    Science Advisor
    Homework Helper

    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook