Link to home
Start Free TrialLog in
Avatar of jasoncpp
jasoncpp

asked on

namespace header files question

I have used c++ a few months now and always create a cpp file and a header file separately.  If a class then needs a header from elsewhere I include the other class in its header file, but this has caused a problem.  How should I be doing this?  I am using visual c++ 6.0 and for example if I have class Person and need class Record, Record may use class Silly.  But I need to include class Thing in Person too, but Thing also includes Silly.  Seen as I need Silly in Record and Silly in Thing, how can I include both classes in Person without redefining Silly?

This may be a silly problem but has not come up before in my small programs.  How can I avoid it?  I have tried moving the include statements into my cpp files but then the compiler complains my header file does not define those classes where needed.

What should I be doing?
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of _corey_
_corey_

Why don't you just give each class a header file.

Do this in each header:

#ifndef _HEADERNAME_H
#define _HEADERNAME_H


#endif //_HEADERNAME_H

to keep from redefining.

If you need to include a class inside of a HEADER, do it this way:

class CLASSNAMEYOUWANTTOINCLUDE;

Now, this forces the .cpp file to include all the proper headers and you don't have to worry about it in the header themselves.
>>Why don't you just give each class a header file

Ooops, I was assuming that already was the case...
Avatar of jasoncpp

ASKER

>>Why don't you just give each class a header file

I do create a class and header file.  Each class has an include statement for its header file, which may include include statements for other classes header files.  Still trying to figure out what jkr originally suggested...I'll go run it.
I'm not sure exactly how your headers are setup still but do it this way.

1 header file for 1 class.

If you need to include other classes in the header, use the method above.

Actually jkr, pretty much has this explained(don't mean to restate you here jkr), I just thought it was hard to read with all the files in sequence.
>>I just thought it was hard to read with all the files in sequence

Thus the link to the website :o)
Thanks, did what corey said and the problem was solved.  Not sure what you were getting at jkr, was it basically the same advice as what corey said?  I did not understand it if it was, so let me know, then I can close the question appropriately.
Ah, just refreshed the page, now I see what's going on.  I think it is fair if I split the points - 250 each, first for being the first to explain, the second for explaining it in baby terms :)
I can feel the glare jkr is giving me :)  Don't give me the points, I'm not here for points or stealing them.
ok, no problem.  I just did not want to be unfair in case I don't get help in the future :P  Thanks.