Thread: how to define a static member in a template class

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    1

    how to define a static member in a template class

    hi,

    i have trouble to define a static member in a template, and i search from google, but didn't find any similar case, my case is a little complex, i paste the code below.

    code in xxx.h

    Code:
    template <class T> class FreeListManager
    {
     public:
      FreeListManager(int s=0);
      ~FreeListManager();
      T* acquire();
      void release(T*);
    
     protected:
      static list<T*> freeList;
      static list<T*> usedList;
      // We have size parameters separated from list, since
      // size() function of list container is inefficient.
    
    };
    code in xxx.cpp

    Code:
    include "xxx.h"
    
    template<class T> list<T*>
       FreeListMaager<T>::freeList;
    template<class T> list<T*>
       FreeListManager<T>::usedList;
    
    //there is no T defined
    for the xxx.cpp file, actually, it is confused me a lot, and it seem to be not what i want, what is the type of the entry in freeList like after above definition? i got coredump when use the freeList.pushback. and i ever tried to define the two list static member like


    Code:
    template<class CLASSA> list<CLASSA*>
       FreeListManager<CLASSA>::freeList;
    template<class CLASSA> list<CLASSA*>
       FreeListManager<CLASSA>::usedList;
    
    template<class CLASSB> list<CLASSB*>
       FreeListManager<CLASSB>::freeList;
    template<class CLASSB> list<CLASSB*>
       FreeListManager<CLASSB>::usedList;
    i got redefinition for freeList and usedList error.
    i was told FreeListManager<CLASSA> and FreeListManager<CLASSB> are totally two different class, why does the redefinition error come out?

    i also try


    Code:
    list<CLASSA*> FreeListManager<CLASSA>::freeList;
    list<CLASSA*> FreeListManager<CLASSA>::usedList;
    
    list<CLASSB*> FreeListManager<CLASSB>::freeList;
    list<CLASSB*> FreeListManager<CLASSB>::usedList;
    i got too few template-parameter-lists error, the code can be compiled on gcc earlier version,

    he code was download from opendiameter project, it can be compiled by very old version gcc complier.

    but i found it cannot compliled by gcc-4.1.2

    As i know, FreeListManager is a only template, and FreeListManager<CLASSA> is actual class, and FreeListManager<CLASSB> is another one, i want to define two static list member for both class,FreeListManager<CLASSA>::freeList, FreeListManager<CLASSB>::freeList, so the two static member should be not one instance, but two. Just like what i tested


    Code:
    template <class T> class FreeListManager
    {
     public:
      static int counter;
    };
    
    template <class T> int FreeListManager<T>::counter=0;
    //the following definition will report redefinition error, either
    //template <CLASSA> int FreeListManager<CLASSA>::counter=0;
    //template <CLASSB> int FreeListManager<CLASSB>::counter=0;
    //i thought the complier only can initial the two static member once, and you can set other value later.
    
    FreeListManager<CLASSA>::counter=2
    FreeListManager<CLASSB>::counter=3
    
    main()
    {
       printf("FreeListManager<CLASSA>::counter=%d\n",FreeListManager<CLASSA>::counter);
       printf("FreeListManager<CLASSB>::counter=%d\n",FreeListManager<CLASSB>::counter);
    }
    ##########################
    the result is
    FreeListManager<CLASSA>::counter=2
    FreeListManager<CLASSB>::counter=3

    so it seem there are two static variable FreeListManager<CLASSA> and FreeListManager<CLASSB>

    can someone help me, really appreciate your help.
    Last edited by wanziforever; 10-08-2009 at 03:46 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Without templates your code would look like:
    Code:
    class foo {
        static list<foo*> foolist;
    };
    
    list<foo*> foo::foolist;
    Therefore with templates your code would look like:
    Code:
    template <class T>
    class foo {
        static list<T*> foolist;
    };
    
    template <class T>
    list<T*> foo<T>::foolist;
    (EDIT: The point being, that you don't actually specify the instantiations yourself. In your example that you posted above (the third block of code) the two things you have are identical (template doesn't care about the names you used) hence a double definition error.)
    Last edited by tabstop; 10-08-2009 at 04:03 AM.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think there might be a problem trying to separate a template class between header and source file.

    i got redefinition for freeList and usedList error.
    i was told FreeListManager<CLASSA> and FreeListManager<CLASSB> are totally two different class, why does the redefinition error come out?
    CLASSA and CLASSB are here just placeholders for what the real type is going to be. Naturally it doesn't matter how you name that and they are identical.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Code:
    template <typename T>
    class ABC {
    private:
       static int m_instance;
    
    public:
       static ABC* Instance();
    };
    
    int ABC::m_instance = 0;
    
    ABC* ABC::Instance() {
    // Define
    }
    I think this can help u out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  3. Can you define a static member function in a class?
    By meili100 in forum C++ Programming
    Replies: 2
    Last Post: 06-22-2008, 09:23 PM
  4. Accessing syscalls from C
    By lilcoder in forum C Programming
    Replies: 17
    Last Post: 09-19-2007, 02:27 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM