initialization of a static member in a nested class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: initialization of a static member in a nested class

  1. #1
    Join Date
    May 2005
    Posts
    33

    Exclamation initialization of a static member in a nested class

    I have a non-template class, with a static data member, nested inside a
    class template. I initialized the static data member as shown below, but it
    produces a compilation error. (I am using Visual.Net.) I wonder if anyone might offer a solution? Thanking in anticipation.

    template <class X>
    class A
    { private:
    class B
    { public:
    static int item;
    .
    .
    };
    };
    template <class X>
    int A<X>::B::item = 3;

    error LNK2001: unresolved external symbol "public: static int A<class
    double>::B::item" (?item@B@?$A@Vdouble@@@@2HA)

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    Do the definition of the static data member and the class template A appear in the same header file? In other words, does

    template <class X>
    int A<X>::B::item = 3;

    Appear in A.h, right after the template body?
    I had no problem compiling this code with C++ BuilderX
    Danny Kalev

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    Some compilers insist that a non-const static data member of a class be defines in a separate .cpp file. So you probably need to add

    template <class X>
    int A<X>::B::item = 3;

    Into one of the .cpp files of yoru projects.
    Danny Kalev

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center