post  Introducing a Template as a parametre of another Template

Ruben (8)   Link to this post
Hi,

I'm trying to define a library which needs an undefined parametre to be passed as a template parametre. This is my code (sumarized):


template <typename V> struct INFO_ELEMENT
{
unsigned char TYPE[4];
unsigned char *LENGTH;
V *VALUE;

public:
INFO_ELEMENT(unsigned int L);

};

template <class V> INFO_ELEMENT<V>::INFO_ELEMENT(unsigned int L)
{
*LENGTH = new unsigned char[1];
LENGTH = (unsigned char)L;
*VALUE = new V[L];

}

template <typename T> struct LIST
{
unsigned char *Length;
T *Value;

public:
LIST(unsigned int L);

};

template <class T> LIST<T>::LIST(unsigned int L)
{
*Length = new unsigned char[1];
Length = (unsigned char)L;
*Value = new T[L];
/*...*/

}

typedef LIST<INFO_ELEMENT>* IR_BIN_DATA;


The error occurs at the last line, where the parametre INFO_ELEMENT passed as the parametre of the instantiation of the class LIST is forbidden. Is is because it expects a type instead of a template.Does anybody know how to solve this issue?

Thanks!
jsmith (3099)   Link to this post
INFO_ELEMENT is itself a template. Your typedef did not specify the template parameter of INFO_ELEMENT.

typedef LIST<INFO_ELEMENT<int> >* IR_BIN_DATA;

works for example.

Ruben (8)   Link to this post
Yes,

I knew it worked, but I left it blank this way "typedef LIST<INFO_ELEMENT>* IR_BIN_DATA;" because I wanted to encapsulate a generic data type. Is it possible? How could I do it?

Thanks!
Last edited on
jsmith (3099)   Link to this post
Unfortunately you can't do that with a typedef.
Ruben (8)   Link to this post
So, is there any other possibility? with or without typedef? anything similar (or not)? any idea?

Thanks!
jsmith (3099)   Link to this post
What about this? Does this work?

1
2
3
4
5
6
7
template< class T > 
class IR_BIN_DATA : public LIST< INFO_ELEMENT< T > >
{ 
   public:
       explicit IR_BIN_DATA( unsigned L ) : 
           LIST< INFO_ELEMENT<T> >( L ) {}
};

Ruben (8)   Link to this post
Thank you jsmith! that's almost what I wanted. I didn't know I could implement it that way.

Thanks a lot!

This topic is archived - New replies not allowed.