Player* HashTable::retrieve(char * key, Player& aPlayer)
{
//calculate the retrieval position (the index of the array)
size_t index = calculateIndex(key);
//search for the data in the chain (linked list)
node * curr = table[index];
char id[100];
while (curr)
{
curr->item.GetName(id);
if(strcmp(key, id) == 0)
{
//find match and return the data
aPlayer = table[index]->item;
pt = &aPlayer;
return pt;
}
else
curr = curr->next;
}
}
Here I'm calling the retrieve and I need to return a pointer of that object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Player* PlayerDB::FetchPlayer(char* name)
{
Player* info = new Player();
out << "Fetching player " << "\"" << name << "\" -- ";
if(h.retrieve(name, *info))
{
out << "Success!" << endl;
return info;
}
else
{
out << "Failed." << endl;
delete info;
return NULL;
}
}
I do have a few observations.
1. retrieve() takes a reference to a Player and return that player if it existed. You have two parallel objects representing any given player; one in the hash, one copied from that hash. Why?
2. If you're using strings, why not use the string class? Having chosen to use C strings, you should be passing constcharchar* around, not char*.
3. Why implement your own linked list when you can use the standard one?
I've been told I need to return a reference of the object in my retrieve function
Hi,
What do you mean by I have two objects representing any given player?
My problem with the program is that any updating I do to the data in one of the Player objects doenst actually get updated.
I have to implement my own table and linked list.
I just dont know how to search for the data and update it.
This is more a data structures issue than a C++ coding issue. Once you've decided what data structures you want, you then think about how you code that, not the other way around.