There can be multiple declarations for classes, structs, variables and such in different header files. However, it is an error if multiple definitions of some language constructs are present. This is covered by the One Definition Rule of C++:
https://en.wikipedia.org/wiki/One_Definition_Rule
If the above link isn’t clear enough, rules of thumb will usually suffice. Some things are allowed and others aren’t. If you get an error, move things around.
Missing Guard
If the compiler reports a multiple definition or duplicate definition error, first make sure that each header file has an appropriate #include guard, (sometimes called a macro guard, header guard or file guard). That’s the macro that looks something like:
#ifndef ARRAYLIST_H
#define ARRAYLIST_H
...bunch of header file stuff...
#endif /* ARRAYLIST_H */
This prevents the same header file from being used in the compile process even though it may be included multiple times from the cpp and other header files. Ensure that the guard is not missing or improperly named. Guards are usually named in some way related to the header’s filename for clarity.
Function Defined in Header File Outside of Class Declaration
add text here
Explicit (full) Specialization Function Defined in Header File Outside of Class Declaration
The use of template<> is not allowed in a header file…move it to the cpp file!
This is because since it is fully specialized (all parameters specified) then it is now an actual function and no longer requires a template class for definition…blah, blah, blah. Who cares? The compiler doesn’t like it so don’t do it.
The compiler treats fully specialized functions as ordinary non-template, non-inline, non-static functions…none of which can be placed in a header file which will be included by multiple compilation units.
#ifndef ARRAYLIST_H
#define ARRAYLIST_H
template<typename tT, bool tIsPrimitive = true>
class ArrayList
{void print();
};// end of class ArrayList
THE FOLLOWING IS NOT ALLOWED IN THE HEADER FILE!
template<>
void ArrayList<char, false>::print()
{
}
THE FOLLOWING IS ALLOWED IN THE HEADER FILE
(it is not fully specialized)
template<typename tT, bool tIsPrimitive>
void ArrayList<tT, tIsPrimitive>::print()
{
}
#endif /* ARRAYLIST_H */
Links
One of the more complete
https://en.cppreference.com/w/cpp/language/template_specialization
https://www.learncpp.com/cpp-tutorial/13-5-function-template-specialization/