Use of the template class-it is best to put the template class together with the implementation in. h

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.