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++
// 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++
// 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.
- Let there is a header file named pentagon.h as below:
C++
// Function to get the side of
// the pentagon
int getting_pentagon_side()
{
// Return the side
return 5;
}
- Let there be another header file named "mathematics.h" that includes the header file "pentagon.h".
C++
// Including another header file
// "pentagon.h" in the current program
#include "pentagon.h"
- Now, a C++ program is created with the name "pentagon.cpp" as shown below:
Program 3:
C++
// 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:
- 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++
// 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:
Similar Reads
include guards in C++
While programming in C++ we often use a class multiple times, and hence it requires to create a header file and just include it in the main program. Now, sometimes it happens that a certain header file directly or indirectly get included multiple times, then the class declared in the header file get
4 min read
<type_traits> Header in C++
The <type_traits> header in C++ is a part of the metaprogramming library introduced in C++ 11. It is a collection of tools to help our code work with different data types. It contains Helper Classes which are standard classes that assist in creating compile-time constants, Type Traits, classes
7 min read
Helper Function in C++ Classes
In C++, the users can keep their class methods organized and readable with the help of private helper functions. Helper functions are utility functions that are only accessible within the class. They can't be called externally but can be used by the class's public methods to perform lower-level task
3 min read
std::greater in C++ with Examples
The std::greater is a functional object which is used for performing comparisons. It is defined as a Function object class for the greater-than inequality comparison. This can be used for changing the functionality of the given function. This can also be used with various standard algorithms such as
2 min read
imag() function in C++
The imag() function is defined in the complex header file. The imag() function is used to find the imaginary part of the complex number. Syntax: template<class T> T imag(const complex<T>& z); Parameter: This method takes a mandatory parameter z: which represents the given complex num
2 min read
Container Adapter in C++
The container adapters are a part of the C++ standard library that gives us a way to modify or adapt existing container classes to suit specific needs or requirements. In this article, we will learn about container adapters in C++. What is a Container Adapter in C++?In C++, container adapters are sp
5 min read
How to Declare an Array in C++?
In C++, an array is a collection of similar data types in which elements are stored in contiguous memory locations. In this article, we will learn how to declare an array in C++. Declaring an Array in C++In C++, we can declare an array by specifying the type of its elements, followed by the name of
2 min read
exit(1) in C++
In C++, the exit function allows the users to terminate the execution of a program immediately and return the control to the operating system. In this article, we will learn about exit(1) in C++. What does exit(1) mean in a C++ Program?The exit() function defined in the <cstdlib> header in C++
3 min read
How to Declare a List in C++?
In C++, list is a data structure used to store elements sequentially in non-contiguous memory locations. This container implements doubly linked list which contains pointers to both previous and next elements in the sequence. In this article, we will learn how to declare a list in C++. Declare a Lis
2 min read
C++ Getters and Setters
In C++, getters and setters are part of data encapsulation used to protect our data, particularly when creating classes. These are public methods that are used to access and modify the private or protected members of a class. In this article, we will learn how to use getters and setters in C++. Gett
2 min read