BUG: LNK2001 Error for Static Member in Class Template

Last reviewed: July 24, 1997
Article ID: Q151167
The information in this article applies to:
  • Microsoft Visual C++, 32-bit Edition, versions 2.0, 2.1, 2.2, 4.0, 4.1, 4.2, 5.0

SYMPTOMS

Linker error LNK2001 is generated for static member variables of nested classes in class templates. The message states:

   error LNK2001: unresolved external symbol "public: static int
   Outer<int>::Inner::sobj"(?sobj@Inner@?$Outer@H@@2HA)

RESOLUTION

Do not use static member variables in nested classes of class templates.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

The sample code below gives the error message LNK2001.

Sample Code

   /* Compile options needed: none
   */

   template<class T> class Outer
   {
   public:
       static T souter;

       class Inner
       {
       public:
           static T sobj;
       };
   };

   template <class T> double Outer<double>::souter = 1;

   template <class T> int Outer<int>::Inner::sobj = 5;

   void main()
   {
       double dval = Outer<double>::souter;

       int i = Outer<int>::Inner::sobj;
   }
 

	
	


Keywords : CPPIss vcbuglist400 vcbuglist500
Version : 2.0 2.1 2.2 4.0 4.1 4.2 5.0
Platform : NT WINDOWS
Issue type : kbbug


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 24, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.