Template class - unresolved external symbol(s) [duplicate]

和自甴很熟 提交于 2020-01-21 03:51:10

问题


I get this error a LOT, and i never know why. Can someone help me find the cause of it?

Edit:Removed code


回答1:


Put the implementation (your method definitions) into the header along with the class declaration (see this in the C++ FAQ). Some compilers have supported an "export" keyword for doing it the way that you did, but that has been nixed in the C++0x.




回答2:


Templates must be defined in header files. See these FAQs for more info:

  • Why can't I separate the definition of my templates class from its declaration and put it inside a .cpp file?
  • How can I avoid linker errors with my template functions?



回答3:


This errors are happening because, your template definitions are not visible to the user code. Template definition should be declared in,

  1. Header file, along with the template declarations
  2. In the .cpp file, where the user code uses it. However in this approach the definition will be visible only to the .cpp which uses it. See below example

test.h

template<typename T>
void foo (T*);

test.cpp

int main ()
{
  foo(1);
}

template<typename T>
void foo (T *p)
{
 ...
}

test2.cpp

// This file can not see the definition of foo


来源:https://stackoverflow.com/questions/5776862/template-class-unresolved-external-symbols

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!