Why won't my template code link when it's outside of my .h file?

By: Vincent Drake

Abstract: Describes why template implementations cannot be separated from their definitions.


Why won't my template code compile? I have the implementation and definition in separate source and header files.


When the compiler sees a template being used, it must know the implementation of the template in order to create a template instance. When you have the template definitions "#included" in your module, the compiler sees nothing of the template implementations and cannot create the template instances.

The only solution at this time is to either #include the implementation file (CPP) into your code before your code makes use of the templates, or keep the implementation inside the header file.

The C++ standard defines the keyword "export" that solves this problem, but this keyword is not supported at the time of this FAQ (current version is C++ Builder 4), nor are there any major C++ compilers that support this keyword.

Server Response from: ETNASC03