为什么模板在.H

为什么模板在.H

问题描述:

可能重复定义:
Why should the implementation and the declaration of a template class be in the same header file?
Why can templates only be implemented in the header file?为什么模板在.H

我真的不明白为什么它olbiged在.h文件中定义模板实现?

有人可以给我一个详细的答案?

谢谢!

因为编译器需要代码来实例化模板。如果将模板实现放入.cpp文件中,则只能在该.cpp文件中实例化模板。