0

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.

3
Contributors
3
Replies
5
Views
5 Years
Discussion Span
Last Post by Fbody
Featured Replies
  • Inclusion guards are a good example of usage. [url]http://en.wikipedia.org/wiki/Include_guard[/url] Read More

  • 1

    It's better than nothing, but no, it's not the better method. Different compilers use/understand the #pragma directive differently. The use of [iCODE]#pragma once[/iCODE] 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. Read More

0

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

1

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 by Fbody: n/a

Votes + Comments
thanks I get it
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.