Nested class in a template

  • Thread starter André Luiz Carvalho
  • Start date
A

André Luiz Carvalho

Hi there,

I'm porting an application from Java to C++ / BREW so I can't use the
stl, therefore, I have to implement the basic structures that the app
use in Java. I'm implementing a Hashtable template, but I get a weird
error from GCC 4.0.0 (apple):

Hashtable.h:153: error: expected constructor, destructor, or type
conversion before '*' token

The code is:

#ifndef _HASHTABLE_H_
#define _HASHTABLE_H_

#include "Vector.h"

template <class K, class V>
class Hashtable
{
public:
typedef unsigned int (*HashFuncPtr)(K *);
typedef bool (*EqualsFuncPtr)(K *, K *);

Hashtable(HashFuncPtr hashFuncPtr, EqualsFuncPtr equalsFuncPtr,
bool useFreeKey = false, bool useFreeValue = false,
const int hashSize = 20);
~Hashtable();
void clear();
int size();
bool containsKey(K* key);
V* put(K* key, V* value);
V* get(K* key);
void remove(K* key);

private:

class HashItem {
private:
bool freeKey;
bool freeValue;
public:
K *key;
V *value;
HashItem( K *k, V *v, bool useFreeKey, bool useFreeValue)
{
key = k;
value = v;
freeKey = useFreeKey;
freeValue = useFreeValue;
}
~HashItem()
{
if (freeKey)
FREE(key);
else
delete key;
if (freeValue)
FREE(value);
else
delete value;
}
};

// Private method
HashItem * findItem(K* key);

// Private members
HashFuncPtr hashFunc;
EqualsFuncPtr equalsFunc;
Vector<HashItem> **hash;
int hashSize;
int curSize;
bool freeKey;
bool freeValue;

};

template <class K, class V>
Hashtable<K, V>::Hashtable(HashFuncPtr hashFuncPtr, EqualsFuncPtr
equalsFuncPtr,
bool useFreeKey, bool useFreeValue, const int
hashArraySize)
{
hashFunc = hashFuncPtr;
equalsFunc = equalsFuncPtr;
hashSize = hashArraySize;
freeKey = useFreeKey;
freeValue = useFreeValue;
hash = new Vector<HashItem>* [hashArraySize];
for (int i = 0; i < hashSize; i++)
hash = new Vector<HashItem>(4, 4);
curSize = 0;
}

template <class K, class V>
Hashtable<K, V>::~Hashtable()
{
for (int i = 0; i < hashSize; i++) {
hash->freeAll();
delete hash;
}
delete hash;
}

template <class K, class V>
void Hashtable<K, V>::clear()
{
for (int i = 0; i < hashSize; i++)
hash->freeAll();
curSize = 0;
}

template <class K, class V>
int Hashtable<K, V>::size()
{
return curSize;
}

template <class K, class V>
bool Hashtable<K, V>::containsKey(K* key)
{
if (findItem(key) != NULL)
return true;
return false;
}

template <class K, class V>
V* Hashtable<K, V>::put(K* key, V* value)
{
HashItem *item = findItem(key);
if (item) {
V* v = item->value;
item->value = value;
return v;
} else {
unsigned int idx = hashFunc(key) % hashSize;
hash[idx]->insert(new HashItem(key, value, freeKey,
freeValue));
}
return NULL;
}

template <class K, class V>
V* Hashtable<K, V>::get(K* key)
{
HashItem *item = findItem(key);
if (item)
return item->value;
return NULL;
}

template <class K, class V>
void Hashtable<K, V>::remove(K* key)
{
unsigned int idx = hashFunc(key) % hashSize;
Vector<HashItem> *h = hash[idx];
int vsize = h->size();

for (int i = 0; i < vsize; i++) {
if ( equalsFunc( h->elementAt(i)->key, key ) ) {
delete h->removeElementAt(i);
return;
}
}
}

template <class K, class V>
Hashtable<K, V>::HashItem* Hashtable<K, V>::findItem(K* key)
{
unsigned int idx = hashFunc(key) % hashSize;
Vector<HashItem> *h = hash[idx];
int vsize = h->size();

for (int i = 0; i < vsize; i++) {
if ( equalsFunc( h->elementAt(i)->key, key ) )
return h->elementAt(i);
}
return NULL;
}

#endif

---

Can anyone help me, please?

Thank you,
André Carvalho.
 
K

kwikius

Hi there,


template <class K, class V>
Hashtable<K, V>::HashItem* Hashtable<K, V>::findItem(K* key)

should be :

///***********************************************************
template <class K, class V>
typename Hashtable<K, V>::HashItem* Hashtable<K, V>::findItem(K* key)
///***********************************************************

regards
Andy Little
 
A

André Luiz Carvalho

should be :

///***********************************************************
template <class K, class V>
typename Hashtable<K, V>::HashItem* Hashtable<K, V>::findItem(K* key)
///***********************************************************

regards
Andy Little

That worked just fine!

Thank you very much,
André Carvalho.
 

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
473,930
Messages
2,570,072
Members
46,522
Latest member
Mad-Ram

Latest Threads

Top