   C++ Header files...
  Ok so i read through that very thourough and very well writting intro on header files and I find myself having one more question i couldn't/didn't see answered.I also wondered someone somewhere stated something about precompiled headers shouldn't be used (using MSVS 2008 Express C++), thoughts?Link to the article: http://www.gamedev.net/reference/articles/article1798.aspFrom the article:Quote:These header files become the interface between your subsystems. By #including a header, you gain access to all the structure definitions, function prototypes, constants etc for that subsystem. Therefore, every source file that uses sprites in some way will probably have to #include "sprite.h", every source file that uses sound may need to #include "sound.h", and so on. Note that you use quotes rather than angular brackets when #including your own files. The quotes tell the compiler to look for your headers in the program directory first, rather than the compiler's standard headers. exactly what should I put in the header?  Something like this?Header A[source lang = "cpp]//psuedo code btw#include <iostream>#include "HeaderB.h"int main();//etc....Header B[source lang = "cpp]#include <iostream>#include "headerA.h"int main(); // would this be redundant?would i needt to define the prototypes and int, booleans in Each header or just one and simply #include "HeaderA.h" and that would do it?[Edited by - Fussion289 on March 6, 2009 2:02:17 PM]
  You shouldn't include headers inside of headers. Just include them in .cpp files. If you do include headers inside of headers you can get some weird errors. Example Blah1.h#include "blah2.h"// stuffBlah2.h#include "blah1.h"// stuffMain.cpp#include "blah1.h"#include "blah2.h"// more stuffI did something like that once and the compiler got stuck in a loop of including. So that is something to look out for.You normally put the declarations of the functions and/or classes in the header and the implementation in the .cpp file. Something like myClass.hclass myClass{void myMethod();};myClass.cppclass myClass{void myMethod(){// do method}};;Quote:Original post by In_Yack_ModeYou shouldn't include headers inside of headers. Just include them in .cpp files.That's bad advice. Fussion, don't follow it. It commonly makes sense to include headers in headers: That keeps the author of the C++ file from having to remember which headers those headers require, and which headers are required by the headers required by the headers, and which headers.... etc.Quote:If you do include headers inside of headers you can get some weird errors.What is being described is circular inclusion. The article already posted describes how to avoid that situation.As for precompiled headers, they have their benefits and their drawbacks... they're certainly considered useful by many people. Saying "never use them" would be silly.   ;  I know that is pseudocode, but are you calling main(); or are you defining it there? I know my compiler never requires me to call main(); once, let alone twice (once in each header).Use #ifndef in every header to ensure you do not get nasty compilation errors. That is standard. For example myClass.h should look like this:#ifndef _MYCLASS_h_#define _MYCLASS_h_class myClass {void doStuff();};#endifThen myClass.cpp might define what doStuff does and include myClass.h.As long as each header includes what it needs to within it, and has #ifndef to prevent it from being defined multiple times, you do not need to worry too much about in what order you include these files in main.cpp, and having class declarations in multiple files helps you so you don't have to worry about sorting them.A considering I have noticed however is if you have custom classes that each utilize each other in parameters of their member functions. At least one must use only pointers to the other object and a forward declaration for the other object MUST appear before the declaration of the custom class. Also, before the object's member functions utilizing the pointer is defined, the actual header file for the class must be included, at least before any actual manipulation of the object being pointed at can be performed.   ;Quote:Original post by Ruzhyo2000I know that is pseudocode, but are you calling main(); or are you defining it there? I know my compiler never requires me to call main(); once, let alone twice (once in each header).Use #ifndef in every header to ensure you do not get nasty compilation errors. That is standard. For example myClass.h should look like this:*** Source Snippet Removed ***Then myClass.cpp might define what doStuff does and include myClass.h.As long as each header includes what it needs to within it, and has #ifndef to prevent it from being defined multiple times, you do not need to worry too much about in what order you include these files in main.cpp, and having class declarations in multiple files helps you so you don't have to worry about sorting them.A considering I have noticed however is if you have custom classes that each utilize each other in parameters of their member functions. At least one must use only pointers to the other object and a forward declaration for the other object MUST appear before the declaration of the custom class. Also, before the object's member functions utilizing the pointer is defined, the actual header file for the class must be included, at least before any actual manipulation of the object being pointed at can be performed.Long story short i was trying to see how to get back to main(); or more over any prototype I make in another/different .cpp file.  I simply put int main(); in it and i was able to go back and forth no problem although i'm still a n00b and I don't find this a very good way to jump around to the different source files prototypes.  Which is really just the short comings of my understanding of the way the code works/flows.   ;  You don't ever need to prototype main().You don't need to prototype any function to "get back to" it. 'return'ing from a function returns the return value to whatever function called it, without having to know where it's returning to.You should not call main() yourself. main() is... special.Do not use leading underscores on the names for your include guards. Use something like #include HEADER_A_H, not #include _HEADER_A_H. Names with leading underscores are reserved for use by the compiler implementation.You don't "jump around" the source; headers are there to make parts of the source visible to each other. This is a much clearer way of thinking about it. The actual control flow is accomplished by calling functions and returning from them. All these techniques for headers are just there so that you can organize your project instead of having to dump everything in one huge file.   