Using Guards in CPP

Hi, guys!

I have recently started to expand a small program I have written a few days ago and I need to include more header files to improve its readability. However, whenever I use the method I have been shown to in my course it gives some awkward error. I decided to use "#pragma once" instead because my compiler supports it, but I am still curios why the method I was using was not working.

1
2
3
4
5
 #ifndef _ _UNIQUETITLE_H
 #define _ _UNIQUETITLE_H
[code]
 #endif
 


Can someone explain me first what are the two "_ _" in front of the title (I have seen that some use one others are not using any) and why would this give an error (the functions that I use that finds themselves in one of the header files say they are not defined)

Thank you!
Alex
Well, it's a syntax error, so that's why you're getting an error.
A start would be to remove the space between the underscores; the underscores are part of the identifier (name) __UNIQUETITLE_H.

1
2
3
4
#ifndef __UNIQUETITLE_H
#define __UNIQUETITLE_H
...
#endif 


Note that even if the space between the underscores was removed, it's still not entirely correct because any identifiers which contain two underscores in any position are always reserved names.
the thing is if I have only one header file it works fine. When I include more *.h files it starts to give me some errors, but the headers have different names ... i felt I needed to put it out there.

Thanks!
Unfortunately #pragma once is not standard (I wish it was), although it already has wide compiler support.

https://en.wikipedia.org/wiki/Pragma_once

I read it was good practise to use both the pragma and the include guards.

Some people go to extraordinary lengths with the header guards: as well as the class name, they put their initials and a date.

I like to use *.hpp files for headers, it means a C++ header file, as opposed to a C header file. Some people use *.tpp for template header files.

When I include more *.h files it starts to give me some errors, but the headers have different names ... i felt I needed to put it out there.


Can you give some examples of what you used ?
Last edited on
I used it exactly as in the code below and it highlighted the first underscore. Cannot remember what the error was.

1
2
3
4
 #ifndef _ _UNIQUETITLE_H
 #define _ _UNIQUETITLE_H
[code]
 #endif 
mbozzi already explained why that was an error, what were the different headers you are trying to do?
This is a syntax error because of the space between the two underscores.
#ifndef expects a single identifier; here _ and _UNIQUETITLE_H are two different identifiers.
#ifndef A_SINGLE_IDENTIFIER is equivalent to #if !defined(A_SINGLE_IDENTIFIER)

The identifier used as the guard (it is in the global namespace) should not begin with an underscore and should not contain a double underscore.
Some identifiers are reserved for use by C ++ implementations and shall not be used otherwise; no diagnostic is required.

— Each identifier that contains a double underscore __ or begins with an underscore followed by an uppercase letter is reserved to the implementation for any use.

— Each identifier that begins with an underscore is reserved to the implementation for use as a name in the global namespace.

- IS


Something like this would be fine:
1
2
3
4
#ifndef UNIQUETITLE_H_INCLUDED_
#define UNIQUETITLE_H_INCLUDED_
...
#endif // UNIQUETITLE_H_INCLUDED_ 
Topic archived. No new replies allowed.