I am studying further about C++ and come across a topic which describe method to avoid multiple inclusion of header file, which may cause compilational problem.

But I never seen such problem, I've tried to mimic the situation with the below files in Visual C++ 2008 express, no error prompted and run perfectly.

Can anyone tell me?


Thank you.

//header.h
double Square (double x);
double Cube (double x);
//pi.h
double circle_area(double r);
//header.cpp
double Square (double Value)
{
/* Function code */
double SquareReturn;

SquareReturn = Value * Value;

return SquareReturn;
}

/* Function definition */
double Cube (double Value)
{
/* Function code */
double CubeReturn;

CubeReturn = Value * Value * Value;

return CubeReturn;
}
//pi.cpp
#include "header.h"

double circle_area(double r)
{
	double area;
	area = 3.14 * Square(r);
	return area;
}
//main.cpp
#include "header.h"
#include "pi.h"
#include <iostream>

int main (void)
{
double Number;
double SquaredNumber, CubedNumber , CircleArea;

 Number = 5;

 /* Call the function */
 SquaredNumber = Square (Number);
 CubedNumber = Cube (Number);
 CircleArea = circle_area(Number);

 std::cout << "Square of 5 = " << SquaredNumber << std::endl;
 std::cout << "Cube of 5 = " << CubedNumber << std::endl;
 std::cout << "Area of a circle of radius 5 = " << CircleArea << std::endl; 
 return 0;
 }

Dani AI

Generated

Short answer: the code in the first post by does not show a problem because header.h contains only function declarations (prototypes). Repeating compatible prototypes across translation units is allowed, so no error appears. The classical multiple-inclusion problems happen when a header provides definitions (non-inline function bodies, global variable definitions, duplicate class/typedef definitions) or when a header gets pulled in more than once inside the same translation unit via nested includes. As pointed out, include guards stop the same header being processed twice; 's example hints at the global-variable case that actually produces redefinitions.

Use patterns that prevent the errors:

#ifndef MY_HEADER_H
#define MY_HEADER_H

// declarations only

#endif // MY_HEADER_H

(or #pragma once on compilers that support it).

Move real definitions into .cpp files; expose globals with extern in headers and define them once in a single .cpp:

// header.h
extern int counter;

// globals.cpp
int counter = 0;

Notes and troubleshooting: if the compiler complains about "redefinition" or the linker reports "multiple definition of ...", search headers for non-inline function bodies or direct variable definitions and replace them with declarations or extern-definitions. Prefer inline/template/constexpr for functions or variables that must live in a header. Break circular includes with forward declarations. Avoid using namespace in headers and avoid reserved macro names (leading underscores). These steps enforce the One-Definition Rule and are what include guards and header discipline are meant to protect.

Recommended Answers

All 3 Replies

try this

//integer.h
int x;
int y;
//main.cpp
#include integer.h
#include integer.h
#include <iostream>

int main(){
x=10;
y=11;
std::cout<<x<<" "<<y<<std::endl;
    return 0;
}

Programmers use code guards, sometimes called include guards, to avoid the problems with double inclusions. You will find them used in almost (if not all) the standard header files that are installed with your compiler. When used, they would prevent the problem like the example presented by evstevemd, above.

Understood. Thank dudes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.