What are the purposes of using ifndef and define? When should it be used and why?

I made a simple program in class just to go over the basic of Friends in classes but it wouldn't work and I was getting errors all over even though I went through it and everything was correct. One of my headers was being use for its own implementation and included at another header. Once I was able to put ifndef and define in the header that was being used twice , my program worked.

So I'm confused what it did.

Recommended Answers

Inclusion guards are a good example of usage.


Jump to Post

All 3 Replies

So is it better to use #pragma once if we know that the header will be called upon more than once.

It's better than nothing, but no, it's not the better method.

Different compilers use/understand the #pragma directive differently. The use of #pragma once will work on a Microsoft compiler, but it may not work on another one. Using the #ifndef/#define style of guard is the more-universal (portable) method.

commented: thanks I get it +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.