“undefined reference” to a template class function

前端 未结 4 1138
予麋鹿
予麋鹿 2020-12-10 22:50

I am writing a template class for an array of objects, call it arrayobjclass, which holds pointers to other objects, specifically to other arrays in my implementation. The a

相关标签:
4条回答
  • 2020-12-10 23:11

    Define your function templates in the header. Compiler needs to see them.

    Cheers & hth.,

    0 讨论(0)
  • 2020-12-10 23:25

    You can't put template declarations in .cpp files like that. Template declarations and implementation need to be visible in the same translation unit. Put template implementations in headers that you #include directly.

    0 讨论(0)
  • 2020-12-10 23:27

    For anyone passing by

    you can also #include the implementation files in main

    in main:

    #include "arrayobjclass.h"
    #include "arrayclass.h"
    
    #include "arrayobjclass.cpp"
    #include "arrayclass.cpp"
    
    0 讨论(0)
  • 2020-12-10 23:32

    Because templates are compiled when required, this forces a restriction for multi-file projects: the implementation (definition) of a template class or function must be in the same file as its declaration. That means that we cannot separate the interface in a separate header file, and that we must include both interface and implementation in any file that uses the templates.

    From http://www.cplusplus.com/doc/tutorial/templates/

    0 讨论(0)
提交回复
热议问题