Initializing a static element of a template class

T

Tom

Please consider the following code snippet:

template<class T> class lifo
{ public:
static T* Anchor;
T* Next;
};

typedef lifo<int> intlifo;
int* lifo<int>::Anchor = NULL; // error: too few parameter lists
int* intlifo::Anchor = NULL; // error: too few parameter lists

What do i have to do in order to initialize Anchor of lifo<int> ?
Where is a parameter list required ?

(sorry if this is a stupid question; it's the first time i play around with
templates...)

Thanks,

Tom
 
V

Vladimir Jovic

Tom said:
Please consider the following code snippet:

template<class T> class lifo
{ public:
static T* Anchor;
T* Next;
};

typedef lifo<int> intlifo;
int* lifo<int>::Anchor = NULL; // error: too few parameter lists
int* intlifo::Anchor = NULL; // error: too few parameter lists

It is not clear what you tried to do here, and where you placed last 3
lines. See http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8
What do i have to do in order to initialize Anchor of lifo<int> ?
Where is a parameter list required ?

You have to fix the definition of the Anchor pointer, something like this:

template< class T >
T* lifo< T >::Anchor = NULL;

This example works:

template<class T> class lifo
{
public:
static T* Anchor;
T* Next;
};
template< class T >
T* lifo< T >::Anchor = NULL;

int main ()
{
lifo< int > aaa;
int bbb = 3;
aaa.Next = &bbb;
int ccc = 3;
lifo< int >::Anchor = &ccc;
a.Anchor = &ccc;
}
 
T

Tom

Vladimir said:
It is not clear what you tried to do here, and where you placed last 3
lines. See http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

You're absolutely right, i was too fast with copy & pasting. I will improve
next time.
You have to fix the definition of the Anchor pointer, something like this:

template< class T >
T* lifo< T >::Anchor = NULL;

ok, i see i misunderstood something fundamental about templates...
This example works:

template<class T> class lifo
{
public:
static T* Anchor;
T* Next;
};
template< class T >
T* lifo< T >::Anchor = NULL;

int main ()
{
lifo< int > aaa;
int bbb = 3;
aaa.Next = &bbb;
int ccc = 3;
lifo< int >::Anchor = &ccc;
a.Anchor = &ccc;
}

Thanks a lot, Vladimir
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top