I am a beginner with C++. When I write the code sometimes I write #include
and the code works, other times I don\'t write #include
It is not true that the header string is included by other headers. The header string itself only has includes. No Definitions. So all necessary definitions needed for the usage of string are in headers included by the header string. These headers may already be included by other headers. Then everything works. The header ios for example includes stringbuf, which includes ...
Even though you haven't explicitly included string, it has been included because of another standard header you included. For example vector may have included string. When you include vector, every thing from vector will get included in your file.
I think future versions of Cpp should have a include_module or module keyword; which only include a specific module from a file. So if a file has 3 classes we only include the one we need.
for example
-I "../mingw/lib/include"
module <string>
Searches the directory for files that define string class. Compilation would be signicantly slower.
If you use members that are declared inside the standard header string
then yes, you have to include that header either directly or indirectly (via other headers).
Some compilers on some platforms may on some time of the month compile even though you failed to include the header. This behaviour is unfortunate, unreliable and does not mean that you shouldn’t include the header.
The reason is simply that you have included other standard headers which also happen to include string
. But as I said, this can in general not be relied on and it may also change very suddenly (when a new version of the compiler is installed, for instance).
Always include all necessary headers. Unfortunately, there does not appear to be a reliable online documentation on which headers need to be included. Consult a book, or the official C++ standard.
For instance, the following code compiles with my compiler (gcc
4.6):
#include <iostream>
int main() {
std::string str;
}
But if I remove the first line, it no longer compiles even though the iostream
header should actually be unrelated.
As Branko said:
It is possible that other headers that you do include have #include in them.
Let's take a look at iostream
includes:
#include <bits/c++config.h>
#include <ostream>
#include <istream>
if you check istream
you can see some include
so like this, we have:
iostream => istream => ios => iosfwd
and in iosfwd
we have string library! but it's not standard, it's for forward declaration. in iosfwd
we have:
#include <bits/stringfwd.h> // For string forward declarations.
and in stringfwd.h
:
@file bits/stringfwd.h This is an internal header file, included by other library headers. Do not attempt to use it directly. @headername{string}
so, You can use string
without #include <string>
.
If you're just using a pointer/reference to a user defined type, the type only needs to be declared:
class my_class;
void foo(const my_class& c);
But when you're using the value, the compiler needs to know the size and with that the definition of the type.
And keep in mind that standard headers may include other ones, which doesn't automaticly mean that all implementations do that, so you can't rely on that.
Although, there is no direct occurence of #include <string>
in a particular source file, doesn't mean it hasn't been included by another header file. Consider this:
File: header.h
#if !defined(__HEADER_H__)
#define __HEADER_H__
// more here
#include <string>
// ...and here
#endif
File: source1.cc
#include <string>
void foo()
{
// No error here.
string s = "Foo";
}
File: source2.cc
#include <header.h>
void bar()
{
// Still no error, since there's a #include <string> in header.h
string s = "Bar";
}
File: source3.cc
void zoid()
{
// Here's the error; no such thing as "string", since non of the
// previous headers had been included.
string s = "Zoid";
}