473,383 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Nested class in a template

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
2 2289

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andriy Shnyr | last post by:
Let us consider the following nested templates case: template<typename T> class Outer{ public: template<typename U> class Inner{ public:
1
by: Chris Schadl | last post by:
Okay, I'm having a bit of a brain-fart and I can't remember how I would do this. Say I have the following: template <typename T1, typename T2> class A; // Forward declaration of A template...
1
by: nitrogenycs | last post by:
Hello, Code below. I've run into the following problem. I want to pass DCManagedTexture* as a template parameter into the DCDefaultResourceManager template. The DCManagedTexture class is...
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
4
by: Mr Dyl | last post by:
I'm trying to declare the following friendship and VS.Net 2003 is complaining: template <class T> class Outter { class Inner {...} ... }
2
by: pagekb | last post by:
Hello, I'm having some difficulty compiling template classes as containers for other template objects. Specifically, I have a hierarchy of template classes that contain each other. Template...
4
by: rach | last post by:
I just started to learn C++. I copied the following code from a data structure textbook to a ".h" file and couldn't compile it. The code contains three template interfaces. One inherits another. The...
3
by: jdurancomas | last post by:
Dear all, I'm trying to declare the operator++ to a nested class. The nested class is not template but the container it is. The code used in teh sample program is included bellow: ...
5
by: huili80 | last post by:
For example, like in the following, the part commented out was intended as partial spectialzation, but it would even compile. Is it even legal to partially specialize a nested template class...
2
card
by: card | last post by:
Hi everyone, I have a question about referencing a nested class contained within a templated class. Of course the best way to show you is by example. Here's my templated classes: #include...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.