LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-06-2009, 06:44 PM   #1
wanziforever
LQ Newbie
 
Registered: Oct 2009
Posts: 3

Rep: Reputation: 0
how to define a static member in a template class


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:
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? 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,

can someone help me, really appreciate your help.


denny
 
Old 10-07-2009, 03:06 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Are you sure that you want to use static? This means that there is only one instance of the data member; controlled by the class. Should you wish to create two instances of FreeListManager with the same template parameter then you will lose the second list.
 
Old 10-07-2009, 03:26 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
But to address your question first note that for templates the definition and the declaration need to be in the same header file.

I would suggest that you make your lists pointers, thus you will have:

Code:
static list<T*> *freeList;
Initialise it will then be:

Code:
template<class T> list<T*> *FreeListManager<T>::freeList = 0;
 
Old 10-07-2009, 08:07 AM   #4
wanziforever
LQ Newbie
 
Registered: Oct 2009
Posts: 3

Original Poster
Rep: Reputation: 0
sure, that's what i want, the 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.

yes, i include the xxx.h file in the xxx.cpp file.

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>
 
Old 10-07-2009, 08:28 AM   #5
wanziforever
LQ Newbie
 
Registered: Oct 2009
Posts: 3

Original Poster
Rep: Reputation: 0
i also tried

template<> list<CLASSA>
FreeListManager<CLASSA>::freeList;
template<> list<CLASSA>
FreeListManager<ACLASSA>::usedList;
template<> list<CLASSB>
FreeListManager<CLASSB>::freeList;
template<> list<CLASSB>
FreeListManager<CLASSB>::usedList;

i aslo got undefined reference error

Last edited by wanziforever; 10-07-2009 at 08:29 AM.
 
Old 10-08-2009, 02:23 AM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
for the gcc compiler "the definition and the declaration need to be in the same header file." That's NOT include the header file in a source but actually placed in the header file.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
c++: template member function used in a different templated class matze Programming 5 04-10-2008 09:26 AM
How to initialize a static array of a class in a static member function lali.p Programming 9 02-16-2008 09:27 AM
How to define default template in OO-Writer? xpucto Linux - Software 2 11-02-2007 09:08 AM
c++ question, how to define a member array, and it's size, outside of the class dec.. Winter Knight Programming 2 01-23-2007 07:28 AM
Template class with a template member... Nicholas Bishop Programming 3 02-21-2005 08:27 PM


All times are GMT -5. The time now is 12:01 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration