Open In App

Header Guard in C++

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++ program to illustrate error
// caused due to defining same
// function more than once
#include <iostream>
using namespace std;
  
// Function 1
void fool()
{
    cout << "hello";
}
  
// Function 2
void fool()
{
    cout << "hello maddy";
}
  
// Driver Code
int main()
{
    // Function Call
    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++ program to illustrate error
// caused due to defining same
// variable more than once
#include <iostream>
using namespace std;
  
// Driver Code
int main()
{
    // error: note: 'double x' previously
    double x{};
  
    // declared here
    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.




// Function to get the side of
// the pentagon
int getting_pentagon_side()
{
    // Return the side
    return 5;
}




// Including another header file
// "pentagon.h" in the current program
#include "pentagon.h"

Program 3:




// C++ program to illustrate the error
// discussed above
#include <iostream>
  
// Include header files created
#include "mathematics.h"
#include "pentagon.h"
using namespace std;
  
// Driver Code
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:

#ifndef PENTAGON_H
#define PENTAGON_H

int getting_pentagon_side()
{ 
return 5;
}
#endif

#ifndef MATHEMATICS_H
#define MATHEMATICS_H
#include "pentagon.h"
#endif

Below is the program after the above changes to avoid the errors:




// C++ program to illustrate how to
// avoid errors using Header Guard
#include <iostream>
  
// Include header guards in both
// the header files
#include "mathematics.h"
  
// Now, the error will not occur
#include "pentagon.h"
  
using namespace std;
  
// Driver Code
int main()
{
    // Function Call to find the
    // sides of the pentagon
    int i{ getting pentagon side() };
  
    // Print the sides
    cout << "sides in a pentagon is: "
         << i;
  
    return 0;
}

Output:


Article Tags :