1. Place the statements and definitions of the C ++ template class in a file, such as. h or. cpp.
When using it, add # include "template class file name. H (or. cpp.
2. Place the C ++ template class statements and definitions in the. h and. cpp files respectively.
And include # include ". H" in the. cpp file"
However, in use, it may vary with different development environments:
A. under the integrated development environment code: Blocks
Before callingProgramOnly # include "template class. cpp" can be added.
You can also add # include "template class. H" and "template class. cpp" to compile and run
However, if you only add # include "template class. H", it cannot be run and the undefined reference to error may occur.
B. In the Linux GCC environment:
Only # include "template class. cpp" can be added to the calling program to compile and run
Otherwise, if you add # include "template class. H" and "template class. cpp" at the same time, the class will be incorrectly defined repeatedly.
Conclusion: Why do we need to add the # include "template class to the calling program. CPP "That's because only the template class is used. the CPP file is the same as the calling program. only when the CPP file is compiled and run together can the real type of the class be determined and the class can be run successfully without the undefined reference to errors.
"Otherwise, if you add the # include" template class at the same time. H "and" template class. CPP "indicates that the class is incorrectly defined repeatedly." This is true, but in Linux, the IDE (qtcreator) can run normally.