Archived

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

paulcoz

Header and Source files question

Recommended Posts

paulcoz    230
This is probably a dumb question: Are source files supposed to automatically know about code in their headers? I'm finding that unless I #include that.h in that.cpp, that.cpp fails to recognise the class declarations I make in that.h?? This doesn't compile: that.h class that { public: int th; void SetThatMember(int blah); } that.cpp void that::SetThatMember(int blah) { th = blah; } The error I get is: that.cpp - 'that' is not a valid namespace or class name. As soon as I add #include that.h to that.cpp everything works fine. Is it supposed to work like this? I thought that this.h and this.cpp were supposed to automatically know about each other, since you usually want to declare an instance of the that class in another source file (else.cpp) by including that.h only (it is assumed that the implementation in that.cpp is attached when you type thatinstance->SetThatMember(2) in else.cpp!). In my way of thinking, that.cpp is supposed to be the implementation of that.h and thus actually makes the header useful. It seems to me that there's no obvious link from the header to the source apart from the filename (the link is assumed by the compiler) so why does the source require explicit direction back to the header? It's clever one way, but not the other, and seems to create a lot of wasted time as I type an include with the name of the header at the top of an identically named source file? Please explain! Paulcoz. Edited by - paulcoz on October 16, 2001 4:08:20 AM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
The source file for its corresponding header doesn''t automatically know about it. If you have seen this behavior somewhere and are trying to reproduce it, it was probably the particular compiler you were using. In order for that.cpp to know what "that" is, you need to include that.h.
This may seem dumb, but then again its not really that much more work. Also, not all of the stuff in a header file needs to be implemented in a single source file. An example of this is the afx.h and afxwin.h headers in MFC. Each is implemented in multiple source files because of the numerous classes and functionality there, some of which is unrelated, but most of which falls nicely into groups. Why the huge header? All of the code in the header is the basic structure and functionality of MFC, and its easier to include one file than twenty.

Share this post


Link to post
Share on other sites
paulcoz    230
Thanks for the confirmation - I thought I had done something wrong which meant the link between the source and the header wasn''t working.

"There''s no such thing as a stupid question; only stupid people"

Oi!

Paulcoz.

Share this post


Link to post
Share on other sites