Include guards

''Oh no! My code doesn't work! I get an error about multiple class definitions! Here's what my header, myclass.h, looks like:''

// Here is my class, which does things. class MyClass { void DoThings; };

The problem occurs when MyClass is defined twice in a single translation unit, which generally happens when myclass.h is included twice. You don't necessarily need #include "myclass.h" twice in a single source file for this to occur - imagine a case where main.cpp includes a.h and b.h, both of which include myclass.h.

The solution, luckily, is easy.


 * 1) ifndef MYPROJECT_MYCLASS
 * 2) define MYPROJECT_MYCLASS

// Here is my class, which does things. class MyClass { void DoThings; };


 * 1) endif

Every time it includes the file, it checks to see if MYPROJECT_MYCLASS is defined. If it isn't, it defines MYPROJECT_MYCLASS - thus ensuring the body won't be included a second time - and then includes the body. If it is, it just skips the rest of the file (down to the #endif).

MYPROJECT_MYCLASS must be unique, otherwise you'll have bizarre issues with files not being included properly. How you guarantee it's unique is up to you - some people include their name, some people include a file path, some people include a timestamp or a random number. (E.g. AUTHOR_YYYYMMDD_HHMM contains some possibly useful information, isn't too long, and - most importantly - has a great deal of permanent global uniqueness.) Often, people start it with an underscore - however, this is incorrect and shouldn't be done, as those are reserved names.

#pragma once
"#pragma once" or a similar mechanism, when offered by a compiler, provides the following minor advantages to include guards:
 * Reduces the possibility of various errors (e.g. #ifdef instead of #ifndef, forgetting the closing #endif, dependance of the guard name on the header name, repeating the same unique identifier twice)
 * Does not pollute the global namespace with a macro
 * Is slightly more likely to trigger a mechanism within the compiler which will speed up compilation by skipping over the header if it is included s second time
 * Is slightly faster to type

It also has the following disadvantages:
 * Is not standard C++
 * Is not guaranteed to exist on all compilers
 * Is not nearly as well-known or as common as include guards

The channel ops have mixed opinions regarding the use of "#pragma once".