Header Guards in C++ are conditional compilation directives that help to avoid errors that arise when the same function or variable is defined more than once by the mistake of a programmer. According to C++, when a function or a variable is defined more than once, it yields an error. Below is the program to illustrate the same:
Program 1:
C++
#include <iostream>
using namespace std;
void fool()
{
cout << "hello" ;
}
void fool()
{
cout << "hello maddy" ;
}
int main()
{
fool();
return 0;
}
|
Output:
Explanation:
In the above program, a function fool() has been defined twice which is causing a fatal error.
Program 2:
C++
#include <iostream>
using namespace std;
int main()
{
double x{};
int x{ 2 };
return 0;
}
|
Output:
Explanation:
In the above program, the variable x has been defined twice which is causing a fatal error.
These are the most commonly occurring errors and can be fixed with a very basic knowledge of programming. But if a situation arises where a Header file is included in a program and inadvertently a forward declaration of a function is done which has been a part of the already included header file. In that case, the content of the header file is not remembered, no matter whether it’s user-defined or predefined.
So further, a fatal error is encountered which sometimes becomes unresolvable. For this, C++is armed with some preprocessor directives that avoid this error.
- Let there is a header file named pentagon.h as below:
C++
int getting_pentagon_side()
{
return 5;
}
|
- Let there be another header file named “mathematics.h” that includes the header file “pentagon.h”.
- Now, a C++ program is created with the name “pentagon.cpp” as shown below:
Program 3:
C++
#include <iostream>
#include "mathematics.h"
#include "pentagon.h"
using namespace std;
int main()
{
return 0;
}
|
Output:
To solve the error caused above, the idea is to use the concept of “Header Guard” to avoid errors that occur when the same function variable is defined more than once by the mistake of the programmer.
A header guard can avoid such errors by using Conditional Compilation directives. It is a combination of Conditional Compilation directives that protect your header from being included in a program multiple numbers of times.
Syntax:
#ifndef HEADER_H_NAME
#define HEADER_H_NAME
/*...
...*/
#endif
Now, Let’s come to the previous example and see how the error conflict can be resolved:
- First, the “pentagon.h” header file is modified as:
#ifndef PENTAGON_H
#define PENTAGON_H
int getting_pentagon_side()
{
return 5;
}
#endif
- Now the “mathematics.h” header file that includes “pentagon.h” is modified as:
#ifndef MATHEMATICS_H
#define MATHEMATICS_H
#include "pentagon.h"
#endif
Below is the program after the above changes to avoid the errors:
C++
#include <iostream>
#include "mathematics.h"
#include "pentagon.h"
using namespace std;
int main()
{
int i{ getting pentagon side() };
cout << "sides in a pentagon is: "
<< i;
return 0;
}
|
Output:
Last Updated :
02 Nov, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...