C/C++ [C++] Problems with strings MinGW

  • Thread starter Thread starter utkarshakash
  • Start date Start date
  • Tags Tags
    Strings
Click For Summary
The discussion centers on an error encountered while trying to convert an integer to a string using the `to_string` function in C++. The error message indicates that `to_string` is not declared in the current scope. The primary solution offered is to replace `#include <string.h>` with `#include <string>`, as `string.h` is intended for C-style strings and does not support C++ string functions. Additionally, it's recommended to include the `std` namespace to properly access C++ standard library functions. For using C string functions in C++, `#include <cstring>` is suggested as a cleaner alternative to `#include <string.h>`. Overall, ensuring the correct headers and namespace are included resolves the compilation issue.
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?
 
Technology news on Phys.org
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.)
 
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 31 ·
2
Replies
31
Views
3K
Replies
4
Views
5K
  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 0 ·
Replies
0
Views
2K