I'm building a hash table that is actually three hash tables, each one for storage of a specific type, shorts, longs, and c-strings. Such a hash table would make for fats and convenient storage and retrieval of said data types by a string key. It would also be possible to add a method for saving the data to a file to the hashtables destructor, and a method for reloading this information to the constructor. I plan to do this later, then I would have a very simple method of saving information that would keep it's state between application sessions (usefull in most any app, and won't require any external libraries).
Still with me? I have created the basics of this hash table but I'm obviously misunderstanding some critical C++ concept because it spews compiler errors. I suspect the problem lies with the structs. My code is below:
--------------------------
----------
----------
----------
----------
----------
-------
DATA.H:
#define NULL 0; //temp null define, should probably add an #ifndef statement or something to it
class data
{
public:
data(int hashTableLength);
~data();
// Adds a long integer value to the hash table under the 32bit hash key given
void Add(const long value, const char * key);
// Gets a long integer value from the hash table at the given 32 bit hash key
long GetLong(const char * key);
private:
LONG ** longs;
int hashTableLength;
};
typedef struct {
long hash = 0;
LONG * next;
long data;
} LONG;
--------------------------
----------
----------
----------
----------
----------
-------
DATA.CPP:
#include "data.h"
data::data(int hashTableLength)
{
longs = new LONG * [hashTableLength];
//init all the pointers at NULL (to prevent stray pointer errors)
for(int i=0; i < hashTableLength; i++)
{
longs[i] = NULL;
}
//store the length for future usage.
this->hashTableLength = hashTableLength;
}
data::~data()
{
//store the data to disk and free all the memory used
}
// Adds a long integer value to the hash table under the 32bit hash key given
void data::Add(const long value, const char * key)
{
long hash = strHash(key,strlen(key));
int index = (int)(hash % hashTableLength); //0-49 for a length of 50
LONG * entry = longs[index];
while((entry->next != NULL) && (entry->hash != hash))
{
entry = entry->next;
}
//so now, this entry->next pointer is either NULL (making it the lest entry)
//or we've stumbled across an existing entry by this key.
if(entry->hash == hash)
{
entry->data = value;
}
//else we need to add a new entry to the end
else
{
entry->next = new LONG; //add a new struct
entry = entry->next; //set entry to point to the new struct
entry->next = NULL; //set the next pointer to NULL (we're at the end after all)
//set the data for this struct
entry->hash = hash;
entry->data = value;
}
}
long data::GetLong(const char * key)
{
long hash = strHash(key,strlen(key));
int index = (int)(hash % hashTableLength); //0-49 for a length of 50
LONG * entry = longs[index];
while((entry != NULL) && (entry->hash != hash))
{
entry = entry->next; //set entry to point to the next LONG struct
}
if(entry == NULL)
return 0; //not found
else
return entry->data;
}
--------------------------
----------
----------
----------
----------
----------
-------
COMPILER ERRORS:
data.h(19): error C2501: 'data::longs' : missing storage-class or type specifiers
data.cpp(33): error C2039: 'next' : is not a member of 'LONG'
data.cpp(6): error C2065: 'longs' : undeclared identifier
data.cpp(12): error C2109: subscript requires array or pointer type
data.cpp(31): error C2109: subscript requires array or pointer type
data.cpp(33): error C2143: syntax error : missing ')' before ';'
data.cpp(33): error C2143: syntax error : missing ')' before ';'
data.h(19): error C2143: syntax error : missing ';' before '*'
data.h(32): error C2143: syntax error : missing ';' before '*'
data.h(32): error C2501: '$UnnamedClass$0x5a7588c4$
1$::LONG' : missing storage-class or type specifiers
data.h(32): error C2501: '$UnnamedClass$0x5a7588c4$
1$::next' : missing storage-class or type specifiers
data.h(19): error C2501: 'data::LONG' : missing storage-class or type specifiers