Encountered undefined reference to when writing template

Source: Internet
Author: User

In particular, the definition of a function in a template class is not written in the corresponding. h file

Reference: Http://stackoverflow.com/questions/8752837/undefined-reference-to-template-class-constructor

My understanding:

Detach the function declaration and definition as before,But I met a strange mistake. Googlebefore we know: the template class just tells the compiler how to generate the class, and there is no generated class, in the compilationmain.cppthe time,Buildlist<quadptr>class, where the function islist.hThere are no specific definitions, none are generated, and the compilationList.cpp, the compiler does not know which class to generate and does not generate suchpush_backand so on, which leads to the connection,list<quadptr>::p ush_backand there is no corresponding implementation. There are two solutions, one is to write the definition in. hfile, this compilesmain.cppwhen, becauseinclude.h, a specific function is generated, and the other method is toList.cppfile tells the compiler to generate the class,template class List<quadptr>.

Encountered undefined reference to when writing template

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.