You receive a LNK2001 error message when you use the template member functions (239436)
The information in this article applies to:
- Microsoft Visual C++, 32-bit Enterprise Edition 5.0
- Microsoft Visual C++, 32-bit Enterprise Edition 6.0
- Microsoft Visual C++, 32-bit Professional Edition 5.0
- Microsoft Visual C++, 32-bit Professional Edition 6.0
- Microsoft Visual C++, 32-bit Learning Edition 6.0
- Microsoft Visual C++ 2005 Express Edition
- Microsoft Visual C++ .NET (2003)
- Microsoft Visual C++ .NET (2002)
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:
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. REFERENCESFor
additional information, click the article number below to view the article in
the Microsoft Knowledge Base: 128789 BUG: LNK2001 on Member Function When Use Nested Class Template
Modification Type: | Major | Last Reviewed: | 1/5/2006 |
---|
Keywords: | kberrmsg kbtshoot kbLangCPP kbprb KB239436 kbAudDeveloper |
---|
|