c++ - How to defined constructor outside of template class -
this question has answer here:
i linker error if define constructor\destructor of template class outside class. not allowed? use visual studio 2010.
error 1>main.obj : error lnk2019: unresolved external symbol "public: __thiscall tree::~tree(void)" (??1?$tree@h@@qae@xz) referenced in function _main
1>main.obj : error lnk2019: unresolved external symbol "public: __thiscall tree::tree(void)" (??0?$tree@h@@qae@xz) referenced in function _main
in .h file
template <class t> class tree{ public: tree(void); ~tree(void); t x; };
in .cpp file
#include "tree.h" template <class t> tree<t>::tree(void){ } template <class t> tree<t>::~tree(void){ }
in main.cpp file
#include "tree.h" int main(){ tree<int> t; return 0; }
templates need declared , implemented in file include. cannot separate template class declaration , implementation , include header file.
with templates, class not compiled until it's used. there no such thing compiled template class can linked against. each time use template, has compiled different type. , since compiler not have access implementation, not know how compile it...
Comments
Post a Comment