Microsoft KB Archive/239436

= You receive a LNK2001 error message when you use the template member functions =

Article ID: 239436

Article Last Modified on 1/5/2006

-

APPLIES TO


 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 5.0 Professional Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition
 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-



This article was previously published under Q239436



Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code. Microsoft Visual C++ 2005 supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model.



SYMPTOMS
You receive a LNK2001 error message on template member functions unless the member functions are also defined in the same translation unit.



CAUSE
The compiler does not support the use of the "export" keyword as specified in the C++ standard below:



Section 14
Para# 7: Declaring a class template exported is equivalent to declaring all of its non-inline function members, static data members, member classes, member class templates and non-inline function member templates which are defined in that translation unit exported.

Para# 8: Templates defined in an unnamed namespace shall not be exported. A template shall be exported only once in a program. An implementation is not required to diagnose a violation of this rule. A non-exported template that is neither explicitly specialized nor explicitly instantiated must be defined in every translation unit in which it is implicitly instantiated (temp.inst) or explicitly instantiated (temp.explicit); no diagnostic is required. An exported template need only be declared (and not necessarily defined) in a translation unit in which it is instantiated. A template function declared both exported and inline is just inline and not exported.



RESOLUTION
You have the following two options:  Keep the template declaration and definition together in one translation unit.  If you know in advance how your template class will be used in your application, you can perform explicit instantiation for the template class.

For example, for class MyStack declared in Myprog.h template  MyStack; and implemented in Myprog.cpp, the following explicitly instantiates MyStack for int variables: template class MyStack ; 



MORE INFORMATION
Explicit instantiation allows you to create an instantiation of a template class or function without actually using it in your code. The idea is to explicitly instantiate all possible types for the template class in the implementation file. Thus, when the .cpp file is compiled, the code is generated for these instantiations, alleviating the LNK2001 error.

Please refer to the Visual C++ Help for more information regarding explicit instantiation.

