+ 3

Why we use in the header file #ifndef and #define

20th Sep 2016, 12:14 PM
Amit Chauhan
Amit Chauhan - avatar
3 odpowiedzi
+ 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 :(
20th Sep 2016, 1:16 PM
kiwiyou
kiwiyou - avatar
+ 2
To make sure we have not defined a header more than once.
29th Sep 2016, 11:20 AM
Davood Abdollahi
Davood Abdollahi - avatar
+ 1
Thanks for the help..
20th Sep 2016, 2:09 PM
Amit Chauhan
Amit Chauhan - avatar