Header guards and #pragma once

Hello

I was making a program and had defined a file called Constants.h in it. When I tried to include it in main.cpp and in another class declaration player.h, I got an error that varible_xyz was defined more than once.

So, I googled up this issue and came to know that header guards can be used to prevent the inclusion of a file multiple times as this post says: http://www.cplusplus.com/forum/general/71787/ . But how do we use #pragma once in our codes?

I'd be very grateful if someone can give me a simple example or a link where this basic issue is addressed.

Thanks
1
2
3
4
5
6
7
8
9
10
// header file

#pragma once

#include "Stuff.hpp"

// function prototypes

void f1();
void f2();


http://en.wikipedia.org/wiki/Pragma_once
http://stackoverflow.com/questions/1143936/pragma-once-vs-include-guards

Header guards are basically a hack, which consists of using the preprocessor to "cut out" header files if they're included multiple times.
closed account (z05DSL3A)
But how do we use #pragma once in our codes?
1
2
3
4
5
6
7
8
#pragma once
#ifndef _CONSTANTS_H_INCLUDED 
#define _CONSTANTS_H_INCLUDED 

//....

#endif 
//---END OF FILE 
Constants.h
Last edited on
<snip>

(half read the question before replying...)

Andy
Last edited on
Topic archived. No new replies allowed.