This topic is now archived and is closed to further replies.


I just want a small advise...

Recommended Posts

OK, so I have a couple of include files I made myself! Basically they are math stuff and such. And I have that class I made, fPOINT which consists of two floats, x and y. Problem is that all of those includes use fPOINT and... they all declare it... Basically, there is one class I use in ALL includes, but since I never know what include I''m gonna need, I declare it in all of them. Of course, problems starts when I include more than 1 of the files... I tried to make the functions templates so I could declare fPOINT in my main CPP and just pass it in, but templates don''t work very well with includes and so I failed... what''s the best solution? 1. Doing something like this:
#ifndef CLASS_fPOINT
#define CLASS_fPOINT
class fPOINT
 fPOINT(): x(0.0),y(0.0){};
 float x;
 float y;
2. Creating a separate file fPOINT.h with this declaration and making all of my other files include that one file (so if I use 1 file, I include it + fPOINT.h, if I use multiple files, I use them + fPOINT.h). 3. Instead of making my functions get fPOINTs as arguments, make them get float x and float y. this is a horrible idea though, because in one function I pass an array of fPOINT with a random number of variables. So I would now have to put two arrays of Xs and Ys. Also, when returning and fPOINT, I would have to now pass an X and a Y as pointers to change their values... 4. Any better idea ??? Thanks in advance

Share this post

Link to post
Share on other sites
The first one is known as multiple inclusion protection, and is the de facto way to handle the problem you're having. It's pretty much standard (although I don't know how technically accurate that is )

[edited by - Zipster on May 26, 2004 2:02:20 AM]

Share this post

Link to post
Share on other sites
If you''re on a *nix platform, just cat some of the headers in /usr/include. #define __file_h__ is pretty much the defacto standard from my experience, at least on *nix.

Additionally, make sure you really want a header as opposed to merely linking some code.

Share this post

Link to post
Share on other sites