+ 3
Why we use in the header file #ifndef and #define
3 Respuestas
+ 6
to prevent multi-include errors in other files.
here's an example.
imagine that we have a header file "A.h" contains:
class A {};
and "main.cpp" contains:
#include "A.h"
#include "A.h"
int main (void) { return 0; }
and we can say that the content of "main.cpp" after preprocessing is:
class A {};
class A {};
int main (void) { return 0; }
so this code generates error.
but if we add #ifndef and #define at the proper places:
[A.h]
#ifndef A_H
#define A_H
class A {}
#endif
and see the after-preprocessed main.cpp:
#ifndef A_H
#define A_H
class A {}
#endif
//here, A_H has already defined!
#ifndef A_H
#define A_H
class A {}
#endif
int main(void) { return 0; }
so we can prevent errors from multi-include of a header file.
sorry for my bad english :(
+ 2
To make sure we have not defined a header more than once.
+ 1
Thanks for the help..