Reputation: 33823
I have two file, the first file is called main.cpp
and that contains the main function, and the second file is called class.h
and that contains a declaration for a class.
in the same two files I have been included <iostream>
file, because each file needs that file
file.
What I want from the second file is check if file iostream
has been already included within the first file main.cpp
, does not include it again.
What I done
// main.cpp
#include <iostream>
#include "class.h"
//class.h
#ifndef iostream_H
#include <iostream>
#endif
Is that code right?, and how can I make sure that it does not include the file again ?
Upvotes: 2
Views: 168
Reputation: 20553
On top of what Luchian Grigore has said.
External include guards (as those in the OP) violate encapsulation because you need to know the internal detail of the header file, namely, the name of the guard.
In addition, those names are not standardized and, hence, external include guards are not portable.
Therefore, do not use external include guards. Add internal include guards to your own header files and be assured that everyone else (including the Standard Library) does the same.
Upvotes: 4
Reputation: 258678
"What I want from the second file is check if file iostream has been already included within the first file main.cpp, does not include it again." you don't need to - include whatever files you need - don't rely on them being included by other files.
<iostream>
has its own include guards, so even if it is included multiple times, it's okay.
If both class.h
and main.cpp
require <iostream>
to compile, both should include it.
Upvotes: 6