• Advertisement
Sign in to follow this  

Best way of incorporating a set of data structures in a project?

This topic is 4711 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! A simple question really, but I can't think of a simple solution: I have a number of data structures, defines and overloaded operators that I want to use for my entire project (all classes). Now, at the moment I've just chucked them all in a single file, which I include in all my headers (one header file per class). The problem is, when I then include these headers in another file (eg. main.cpp), that file sees multiple definitions and redefinitions of the same structures, which generates a compile error. So what is the usual way of doing this? It must be a fairly common thing to do, yes?

Share this post


Link to post
Share on other sites
Advertisement
If they are in a header file, just do this:


// start of header file
#ifndef _HEADERFILENAME_
#define _HEADERFILENAME_




// very end of header file
#endif



This should prevent what you're talking about.

Share this post


Link to post
Share on other sites
you should use somethine like this, an include guard...

#ifndef HWHATEVER
#define HWHATEVER

//header contents here...

#endif


HWHATEVER can be anything, I ussually prefix with H and the name of the header buts thats all up to you.

Edit: Endar beat me too it...

Share this post


Link to post
Share on other sites
So in my Structs.h file, I just go:


#ifndef Structs
#define Structs

// Data, code and all that

#endif



Does that sound about right?

Share this post


Link to post
Share on other sites
Quote:
Original post by Naigewron
So in my Structs.h file, I just go:

*** Source Snippet Removed ***

Does that sound about right?


I'd personally slap an _H or an _HPP on there (depending on the extension) just to make it unique.

Share this post


Link to post
Share on other sites
Ahhh, of course. It can be anything, since the #define is right there. SOrry about that, my brain was offline for a second.

Thanks guys!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement