[C++] Problems with strings MinGW

  • Context: C/C++ 
  • Thread starter Thread starter utkarshakash
  • Start date Start date
  • Tags Tags
    Strings
Click For Summary

Discussion Overview

The discussion revolves around issues encountered when converting an integer to a string in C++ using the to_string function, specifically in the context of using MinGW as a compiler within CodeBlocks. Participants explore the appropriate headers to include for proper functionality.

Discussion Character

  • Technical explanation

Main Points Raised

  • One participant reports an error stating "to_string was not declared in this scope" when attempting to use the to_string function after including string.h.
  • Another participant suggests that string.h is meant for C-style strings and recommends including #include <string> instead to access C++ string functions.
  • A further reply emphasizes the need to include the std namespace to avoid scope issues, referencing an external example for clarity.
  • One participant reiterates the distinction between C and C++ string headers, suggesting #include <cstring> as a cleaner alternative for C functions in C++ and confirming the necessity of #include <string> for C++ string functions.

Areas of Agreement / Disagreement

Participants generally agree on the need to use the correct headers for C++ string functionality, but there is no consensus on the best practices regarding the inclusion of C-style headers in C++ programs.

Contextual Notes

Some participants note the potential confusion between C and C++ string handling, highlighting the importance of understanding the differences in header files and their intended use.

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.
 

Similar threads

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