Retrieve from Hash Table HELP!

Hey guys,

I'm trying to retrieve the Player object from my hash table so I can edit the content of that object.
Here is what I have.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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;
	}
}


And here is my call in main.
1
2
3
4
5
6
Player* outPlayer = NULL;

	outPlayer = pdb.FetchPlayer("Sappho");
	if (outPlayer != NULL) {
		outPlayer->LevelUp();
	}


I'm just trying to change the actual level of the player using the LevelUp function, but right now it is not making that change.

I've been told I need to return a reference of the object in my retrieve function, but I thought that was what I was doing already.

Please HELP!

Thank you.
It's not clear what the problem is. What's wrong?

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 const char char* 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
I think that goes back to my point 1.
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.

Any help?

Thanks.
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.
Topic archived. No new replies allowed.