As most of you know, you will get 'Fuction already defined' or 'Class already defined' errors if a self-made header is included more than once in a project. The way I have been preventing this thus far is to use a structure like this:
This feels a bit hackish to me. Is there a better way to do it, or how do you personally prevent multiple input from your headers? Thanks for your time.
//All of the function protos, classes, etc.
For asfar as i know this is the best way of doing it. mostly because these commands are interpretid by the compiler itself not during run time or anything alike. it is the most common and best way of doing this for asfar as i know.
I think there might be another command like #pragma once, but AFAIK, using the #ifndef method is the most common way of doing it.
This is what MSVC6.0 generates:
#if !defined(AFX_CALCULATOR_H__16A86382_5D9A_11D7_8F7A _444553540000__INCLUDED_)
#define AFX_CALCULATOR_H__16A86382_5D9A_11D7_8F7A_44455354 0000__INCLUDED_
#if _MSC_VER > 1000
#endif // _MSC_VER > 1000
#endif // !defined(AFX_CALCULATOR_H__16A86382_5D9A_11D7_8F7A _444553540000__INCLUDED_)
The #pragma once directive means the file doesn't have to even be opened again which is quicker than having to reopen the file and testing the symbol everytime the compiler comes across an include for it.
I use gcc.
I tried using #pragma once and it warned me that #pragma once is obsolete...? Hmm.