I've been reading through a lot of the threads here, and one point of confusion that seems to be a re-occurring theme for newer programmers is how to properly include files.
To be honest, I don't know if I do it properly either, so hopefully this will totally clear this matter up.
I've been doing this all using Dev-Cpp.
NOTE: One point I'd like to make, is that there is a BIG DIFFERENCE in how the compiler/linker behaves depending on if you're creating a PROJECT or not.
The PROPER way, and please correct me if I'm wrong, to include source files is as follows:
1) CREATE A NEW PROJECT (whatever type it may be)
2) SEPARATE THINGS MODULARLY (this should be a given)(i.e. don't put 2 different class declarations in the same file, don't put implementations of different classes in the same file, etc.)
3) USE INCLUSION GUARDS ON HEADER FILES:Code:#ifndef __MYCLASS_H
#define __MYCLASS_H
class MyClass
{
/* Your Class Declaration Here */
};
#endif
4) If you're creating a PROJECT, the LINKER will automatically scan for the definition files of a header file. Example:
We have a project with the following files:
main.cpp, MyClass.h, MyClass.cpp
WE INCLUDE LIKE THIS:
main.cpp -needs only to include "MyClass.h"
MyClass.h -doesn't need to include ANYTHING
MyClass.cpp -needs only to include "MyClass.h"
NOTE: If you're not creating a project, the above WILL NOT WORK!!! In this situation, I believe it's impossible to avoid including the source file (.cpp files). So I usually "include" the source file at the bottom of the header file, that way main still only needs the header file.
If anyone has anything to add, or if there are any corrections for this, please post them, not only for others' benefit, but for mine as well!
One question I do have however, is if you have a non-class declaration header file (just functions) should any external dependencies be included in the HEADER or SOURCE file?
For example:
If I have the files: -functions.h and -functions.cpp
and these functions require <iostream> and I'd like to use cout and cin, does the following:
go in the HEADER or SOURCE? I've always placed them in the SOURCE file, it just seems to go with convention.Code:#include <iostream>
using std::cout;
using std::cin;
If anyone has anything to add, or to correct, please do so, for my benefit and for the benefit of others, Thank You!