c ++静态库中的模板方法

时间:2013-01-09 15:12:53

标签: c++ templates

  

可能重复:
  Why can templates only be implemented in the header file?

我在非模板类中编写了模板方法。这个项目是静态库。 然后我编写了测试控制台应用程序,并希望使用此方法。但是存在编译错误(外部未解析)。然后我问google并找到了论坛(http://cboard.cprogramming.com/cplusplus-programming/108544-static-libraries-template-functions.html),其中'matsp'说:“使用模板时,整个模板实现需要在头文件中。”我确实喜欢他说,它很棒。为什么呢?

1 个答案:

答案 0 :(得分:1)

因为它与 C ++ 中的模板一样......他们的定义必须在每个使用的翻译单元中都可见。