By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,122 Members | 2,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,122 IT Pros & Developers. It's quick & easy.

Nested class in a template

P: n/a
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[i] = new Vector<HashItem>(4, 4);
curSize = 0;
}

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

template <class K, class V>
void Hashtable<K, V>::clear()
{
for (int i = 0; i < hashSize; i++)
hash[i]->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.
Aug 3 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"André Luiz Carvalho" <al********@gmail.comwrote in message
news:96**********************************@p25g2000 hsf.googlegroups.com...
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
Aug 3 '08 #2

P: n/a
On Aug 3, 2:57*pm, "kwikius" <a...@servocomm.freeserve.co.ukwrote:
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.
Aug 4 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.