Select the product you need help with
- Internet Explorer
- Windows Phone
- More products
You receive a LNK2001 error message when you use the template member functions
Article ID: 239436 - View products that this article applies to.
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.
You receive a LNK2001 error message on template member functions unless the member functions are also defined in the same translation unit.
The compiler does not support the use of the "export" keyword as specified in the C++ standard below:
Section 14Para# 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.
You have the following two options:
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.
For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
(http://support.microsoft.com/kb/128789/EN-US/ )BUG: LNK2001 on Member Function When Use Nested Class Template
Article ID: 239436 - Last Review: January 5, 2006 - Revision: 5.0