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.

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.

Edited 5 Years Ago by Fbody: n/a

Comments
thanks I get it
This question has already been answered. Start a new discussion instead.