Instanciating template classes

E

Ernesto

Hola gurús:

I'm programming my own collection classes and I already implemented a
DynamicArray defined as:

template <class T>
class DynamicArray
{
...
};

Now, I want to implement a hash table as follows:

template <class KEY_T, class VALUE_T>
class HashTable
{
};

In my hash table, the data will be stored in a DynamicArray that will
contain DynamicArrays containing the respective [key;value] objects.
So, I defined too:

template <class KEY_T, class VALUE_T>
class HashTableKeyValue
{
...
KEY_T key;
VALUE_T value;
};


so, I want to create an attribute inside my hash table like:

DynamicArray < HashTableKeyValue<KEY_T, VALUE_T> > ** dataarray;

i.e., I want my dataarray to contain DynamicArrays that contain
HashTableKeyValues<KEY_T, VALUE_T> ... obviously, it does not compile
because the notation is not correct (the compiler says that it needs a
real type, not a template).

How can I implement this stuff? Do you think my logic is right?
Thanks in advance


Saludos



Ernesto
 
L

Leor Zolman

Hola gurús:

I'm programming my own collection classes and I already implemented a
DynamicArray defined as:

template <class T>
class DynamicArray
{
...
};

Now, I want to implement a hash table as follows:

template <class KEY_T, class VALUE_T>
class HashTable
{
};

In my hash table, the data will be stored in a DynamicArray that will
contain DynamicArrays containing the respective [key;value] objects.
So, I defined too:

template <class KEY_T, class VALUE_T>
class HashTableKeyValue
{
...
KEY_T key;
VALUE_T value;
};


so, I want to create an attribute inside my hash table like:

DynamicArray < HashTableKeyValue<KEY_T, VALUE_T> > ** dataarray;

Did you mean for "dataarray" to point to a 1-dimensional (native)
dynamically-allocated array of DynamicArray objects, or for it to be a
pointer to an array of pointers to DynamicArray objects? As written,
you've specified the latter, but your description sounds like you wanted
the former... (which you'd get by removing one of the *'s)
i.e., I want my dataarray to contain DynamicArrays that contain
HashTableKeyValues<KEY_T, VALUE_T> ... obviously, it does not compile
because the notation is not correct (the compiler says that it needs a
real type, not a template).

I'm trying to figure out what exactly you mean by the KEY_T and VALUE_T in
your definition:

DynamicArray < HashTableKeyValue<KEY_T, VALUE_T> > ** dataarray;

because if you precede that with something like:


typedef int KEY_T;
typedef int VALUE_T;

it does compile; it is legal. But it is certainly confusing to have those
names here /and/ use them as template parameter names in the
HashTableKeyValue template as well...


How can I implement this stuff? Do you think my logic is right?

I'm not a hash table expert so I won't critique your implementation
strategy, but I hope at least what I've pointed out might be useful...
-leor
Thanks in advance


Saludos



Ernesto

Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
L

Leor Zolman

I just realized that I /think/ you were saying you wanted to define
dataarray within the HashTable template...the ordering of your post had me
a bit confused. Well, that works fine. Here's code that compiles ok (just
don't try to link it):

template <class T>
class DynamicArray
{
};

// Now, I want to implement a hash table as follows:

template <class KEY_T, class VALUE_T>
class HashTableKeyValue
{
KEY_T key;
VALUE_T value;
};

template <class KEY_T, class VALUE_T>
class HashTable
{
DynamicArray < HashTableKeyValue<KEY_T, VALUE_T> > ** dataarray;

};


Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
E

Ernesto

Leor Zolman said:
I just realized that I /think/ you were saying you wanted to define
dataarray within the HashTable template...the ordering of your post had me
a bit confused. Well, that works fine. Here's code that compiles ok (just
don't try to link it):

template <class T>
class DynamicArray
{
};

// Now, I want to implement a hash table as follows:

template <class KEY_T, class VALUE_T>
class HashTableKeyValue
{
KEY_T key;
VALUE_T value;
};

template <class KEY_T, class VALUE_T>
class HashTable
{
DynamicArray < HashTableKeyValue<KEY_T, VALUE_T> > ** dataarray;

};


Hi Leor Gurú:

I implemented:

DynamicArray<HashTableKeyValue<KEY_T, VALUE_T>> ** dataarray; //without whitespaces

and my compiler did not understand it!!!!

Your code worked perfectly, thanks a lot!!



Saludos


Ernesto
 
L

Leor Zolman

Hi Leor Gurú:

Well, I make too many posting mistakes to qualify as a guru, unless there's
a special category for "Flaky Guru"...
I implemented:

DynamicArray<HashTableKeyValue<KEY_T, VALUE_T>> ** dataarray; //without whitespaces

and my compiler did not understand it!!!!

You mean the old ">>" without any space between them trick? But the
original code you posted had the space! Just another example of why it is
best to copy-and-paste actual code into messages???
Your code worked perfectly, thanks a lot!!

No problemo,
-lior (Spelled in Spanish, sort of)


Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
E

Ernesto

Leor Zolman said:
Well, I make too many posting mistakes to qualify as a guru, unless there's
a special category for "Flaky Guru"...

You mean the old ">>" without any space between them trick? But the
original code you posted had the space! Just another example of why it is
best to copy-and-paste actual code into messages???

Yes, I added a whitespace to my message but my actual code did not have it.
No problemo,

(i.e. No hay problema)

-lior (Spelled in Spanish, sort of)


Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html

saludos


Ernesto
 
V

Victor Bazarov

Ernesto said:
I'm programming my own collection classes and I already implemented a
DynamicArray defined as:

template <class T>
class DynamicArray
{
...
};

Now, I want to implement a hash table as follows:

template <class KEY_T, class VALUE_T>
class HashTable
{
};

In my hash table, the data will be stored in a DynamicArray that will
contain DynamicArrays containing the respective [key;value] objects.
So, I defined too:

template <class KEY_T, class VALUE_T>
class HashTableKeyValue
{
...
KEY_T key;
VALUE_T value;
};


so, I want to create an attribute inside my hash table like:

DynamicArray < HashTableKeyValue<KEY_T, VALUE_T> > ** dataarray;

So is that going to be like this:

template <class KEY_T, class VALUE_T>
class HashTable
{
DynamicArray<HashTableKeyValue<KEY_T, VALUE_T> > **dataarray;
};

or did you do it differently? If so, how?
i.e., I want my dataarray to contain DynamicArrays that contain
HashTableKeyValues<KEY_T, VALUE_T> ... obviously, it does not compile
because the notation is not correct (the compiler says that it needs a
real type, not a template).

How can I implement this stuff? Do you think my logic is right?

Read the FAQ 5.8


Victor
 

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

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top