By default, in an IDE, a source file is a .CPP file, and a header file is a .H/.HPP file.
Their role in compilation is different.
A cpp file "translates" into an OBJ pre-executable file.
Multiple OBJ files "translate" into an executable file (Only if in one of the OBJ files, the Entry Point is defined, otherwise compilation errors occur).
Fact is, every function and class into a OBJ file must only be present ONCE.
You cannot define main twice, neither myfunction, unless it's an overload.
But, templates are an exception.
They must be in the header file as they MUST be SINGULARLY put into an OBJ file, otherwise.... Well, there's no way around that.
Headers file must contain declarations:
int function();
Source file must contain definitions:
int function() { return 1; }
Templates must be put into header files and must be fully defined:
1 2
|
int function();
template <typename T> void Test(T& t) { t.Test(); }
|