preprocessor directives

Hi. Please clarify. What kind of compilation errors can ocurr if I repeat
the same pre-processor directives (such as # include <iostream>, <string>,
etc.) on a set of two h files, and two cpp files integrated onto one Visual Studio 2010 project window? As opposed to just including the .h file in the
cpp file, eg., #include "Library.h". My understanding is that if I do a
#include "Library.h" in any subsequent files, then all the preprocessor
directive template attributes just carry over from the "base" .h file;
therefore avoiding errors.
Compilations errors should not be a problem here as long as you take the necessary precautions in your headers.

Something like this will ensure that you never declare the same things over and over again:
1
2
3
4
5
//ClassA.h
#ifndef CLASSA_H // First line
#define CLASSA_H
// Everything else goes here
#endif // CLASSA_H // Last line 

This is a professional precaution and if you look through your standard library files (iostream, string) you'll see that those headers all have this.

Another one is to use the #pragma once command but only some compilers support it.

A second recommendation is to ensure that you close all of your brackets before including a new file. Something like this is waiting for disaster:
1
2
3
4
5
namespace something{
#include <iostream>
#include <string>
#include <cmath>
}

If you do that in your header files, then calling the headers from multiple places (including other headers) will kill you.
Oh one more piece of advice:

If you have a class declaration in a header which you forget to terminate with a ; at the end, your compiler will find the error on the next line in each file that included this one. This can actually get VERY difficult to track so if you're running into something very strange, check that.
Topic archived. No new replies allowed.