Header File??

1
2
3
#ifndef HOME_H
#define HOME_H
#include <iostream> 


Hi, I saw some use the line 1 and 2, may I know what's the purpose of doing this? And what is #ifndef and #define means? Thanks :)
#ifndef = if not defined
#define = define

It is for header files to not be included in same file more then once.
This is only relevant for the compiler itself (not part of your program). It avoids including the same header multiple times. If you skip these lines you'll get some error saying that object xyz was defined before....

Actually, at the end of the file you'll find an #endif to go with the #ifndef!!

EDIT: read on "precompiler directives" to go from here...
Last edited on
Hi, thanks for explanation

I'm using Ms Visual Studio C++, so can I ignore them? And just put the #include things inside? Tq :)
We can't know without the preprocessor directives of any of the other files in your project, I'm pretty sure.
You mean need to know every header is it? Then only can confirm whether the above things is needed? Thanks :)
closed account (zb0S216C)
atjm88 wrote:
"And just put the #include things inside?"

Trying to be clever here will only cause problems. If it's a header, include header guards.

atjm88 wrote:
"I'm using Ms Visual Studio C++, so can I ignore them?"

You can, but it would be silly to omit them since they prevent multiple inclusion.

Wazzak
Thanks, is that I can used #pragma once for this also? If yes, how to code it? Is it like this?

#pragma once HOME_H

Thanks :)

Thanks, is that I can used #pragma once for this also? If yes, how to code it? Is it like this?

#pragma once HOME_H

Thanks :)


AFAIK
1
2
3
#pragma once
#include <iostream>
//... 



i'm using Ms Visual Studio C++


does VS prevent multiple inclusion? AFAIK, they're not... in this case, VS doesn't has anything to do with...

CMIIW
Thanks chipp

1
2
3
#pragma once
#include <iostream>
//...  


From the code above, is that means, whatever that is under #pragma once will be only used once? Thanks :)
Topic archived. No new replies allowed.