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;
 }

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.