bool HashTable::retrieve(charconst * const key, Player& aPlayer)const
//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];
if(strcmp(key, id) == 0)
//find match and return the data
aPlayer = curr->item;
curr = curr->next;
//data is not in the table
When the FetchPlayer() returns the pointer it passes that pointer to outPlayer and after I debugged it, it has the correct class object to level up. After outPlayer calls LevelUp(); is where my data gets corrupted. The name of the Player changes and the gender and level of the player changes to the same big negative number, but the level for the player in the database continues to be the default one, 0. So I think it is just never accessing the Player instance.
I believe I'm just not assigning the pointer correctly in my FetchPlayer();
That's the nature of memory leaks and invalid pointers - sometimes you'll be lucky and it'll work, others it will get wrong values, others still it will fall in a steaming heap.
What's happening is the memory used for your local Player info variable within FetchPlayer() is sometimes (once the function returns) being re-used for other storage and sometimes not.
If it's re-used, it'll have new bytes stamped over it and you'll get invalid data.
If it's not re-used, you're lucky and your values will still be there. You still shouldn't be accessing it though because it's no longer allocated.
If you want to return a pointer to a Player you have to dynamically allocate that Player object (and make sure you delete it when you're done)
Player* PlayerDB::FetchPlayer(char* name)
Player* info = new Player();
out << "Fetching player " << "\"" << name << "\" -- ";
out << "Success!" << endl;
out << "Failed." << endl;
Or similar (I haven't tested the above)
And then make sure you delete the outPlayer pointer when you're done.
Keep in mind though, you're now mixing Player pointers in the PlayerDB class and Player references in the HashTable class. While these are interchangeable and can be directly converted, it would be better to use one or the other all the way through, for simplicity's sake.
This is so weird to me. It does work because I tested the level value before and after the LevelUp call, but when I call my output to screen function, PrintDiagnostics(); the value for that Player instance is still 0. Here is my PrintDiagnostics();
ostream& operator<<(ostream& out, HashTable& ht)
HashTable::node * curr;
for(i=0; i < ht.capacity; i++)
out << "Slot [" << i << "]:" << endl;
if(ht.table[i] == NULL)
out << " EMPTY" << endl;
for(curr = ht.table[i]; curr; curr = curr->next)
//we can use << on data object because we overload << in the data class
out << " " << curr->item;
Okay, but what I'm trying to do here is to update the Player instanced searched. So what you are saying is that I'm only updating a temporary value of some sort and not the actual Player object in the Hash Table?
If that is true, then I'd have to remove the Player instance and then insert it again with an updated value, or is there a way to update the level value of a Player instance inside the Hash Table.
From what I can see, your HashTable::retrieve() method needs to change; at the moment it's returning a copy, copied to the aPlayer reference. You need to return a reference to the object, or a pointer to it.
You've also marked HashTable::retrieve() as 'const', which means you can't return anything that can be used to alter the content anyway. You'll need to remove that const tag.
Sorry, I'm a bit pressed for time at the moment so I can't go into too much detail.