Article ID: 151167 - Last Review: December 2, 2003 - Revision: 2.0

BUG: LNK2001 Error for Static Member in Class Template

System TipThis article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.
This article was previously published under Q151167

On This Page

Expand all | Collapse all

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.

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;
}
				

APPLIES TO
  • Microsoft Visual C++ 2.0 Professional Edition
  • Microsoft Visual C++ 2.1
  • Microsoft Visual C++ 2.2
  • Microsoft Visual C++ 4.0 Standard Edition
  • Microsoft Visual C++ 4.1 Subscription
  • Microsoft Visual C++ 4.2 Enterprise Edition
  • Microsoft Visual C++ 5.0 Enterprise Edition
  • Microsoft Visual C++ 4.2 Professional Edition
  • Microsoft Visual C++ 5.0 Professional Edition
Keywords: 
kbbug kbcompiler kbcpponly KB151167
Retired KB ArticleRetired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.