开发者

Template Class Link-Time Error [duplicate]

开发者 https://www.devze.com 2022-12-30 11:45 出处:网络
This question already has answers here: Closed 12 years ago. 开发者_开发知识库 Possible Duplicate:
This question already has answers here: Closed 12 years ago. 开发者_开发知识库

Possible Duplicate:

Why can templates only be implemented in the header file??

Hi,

I have a c++ project which consists of several header and source files. In order to modify the class with a template, I found that all the method definitions must be included in the header file itself, else resulting in a link-time error. What is the reason for this and how can I get over this ??

Thank You!!


This is intentional. Templates go in header files, end of story. There is no way around it. In order for the compiler to instantiate the template it must be able to see it, which means it must be in a header file. Templates are not like C# generics, where the model allows them to be hidden an essentially instantiated by the linker - templates are much more complicated than that, so they have to live in header files.

In your travels you will find mention of the export keyword, which was supposed to solve this. To cut a long story short, it didn't, no one implemented it (apart from one very brave compiler vendor) and it will soon not be part of C++ any more.

0

精彩评论

暂无评论...
验证码 换一张
取 消