Exercise with multiple includes

Hi!I'm new here and I'm an absolute beginner and I'm reading a book here about C++ and there's a topic about multiple includes and header files.There's an exercise where I have to
include the Header.h in various files and see what error it produces.
The .h file has two function prototypes about Square and Cube.I thought it'd be easy and did a simple program with Square and Cube calculations in two source files.In both files I included the header and I thought that it'd produce an error - but it didn't.How can I produce that error without much effort?

PS:sorry if that's too easy for somebody but I started yeasterday :)
I don't know what error it's referring to. Headers are supposed to be included in multiple files, it's kind of the whole point.

perhaps if you could show us the code that's in the header?
The topic's name in the book is "Avoiding Multiple includes".There are few directives like #pragma and #ifdef briefly explained and there's an example given with a header file and two source files that calculate the square and the cube of a number.If you need the code here it is:
01 Header.h
1
2
double Square (double x);
double Cube (double x);


01 Header.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "01 Header.h"
double Square (double Value)
{     double SquareReturn;
	SquareReturn = Value * Value;
	return SquareReturn;
}
double Cube (double Value)
{      double CubeReturn;
	CubeReturn = Value * Value * Value;
	return CubeReturn;
}


01 Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "01 Header.h"
#include <iostream>
main ()
{      double Number;
	double SquaredNumber,CubedNumber;
	std::cin>>Number;
	SquaredNumber = Square (Number);
	CubedNumber = Cube (Number);
	std::cout<<"Square of "<<Number<<" is: "<<SquaredNumber<<std::endl;
	std::cout<<"Cube of "<<Number<<" is: "<<CubedNumber<<std::endl;
	return 0;
}

And in the chapter there's this:

If you wanted to use the functions implemented in Header.cpp in
various files, you would have to include Header.h in those files. Doing this would
create a linker error since it was trying to implement the functions in Header.cpp
various times.


Then there's an exercise that says:

Without doing multiple includes prevention, try to include Header.h in various files and see what error it produces.

Perhaps I have to produce that error and as you can see in the code I have included the header "in various files" as the task says and nothing.It sounds a simple task to me, but what can I do about it?
Last edited on
If you wanted to use the functions implemented in Header.cpp in
various files, you would have to include Header.h in those files. Doing this would
create a linker error since it was trying to implement the functions in Header.cpp
various times.


This is simply not true. In fact it's the exact opposite of true. This is exactly what you are supposed to use headers for. It kind of sounds like it's talking about #include guards, but the way it's worded there is extremely poor. And there is absolutely nothing wrong with that example code you showed.

Anyway, this book sounds like crap. Here's an article I wrote on includes and headers. Maybe you'll find it to be a bit more clear:

http://cplusplus.com/forum/articles/10627/
Last edited on
Thanks for the reply.
Last edited on
Topic archived. No new replies allowed.