Storing Objects in a Linked List

Started by
10 comments, last by BitMaster 18 years, 8 months ago
Hey all. Um having a bit of problem trying to make an object with all its member data placed into a linked list so i can access it all using Nodes. i have the following code:

           //kill, username, email, kill count
soldier Bob(1,"Twinsen","Twhj@jher.com",2345);
constructor:

soldier( int rank, std::string NickName, std::string EmailAddress, int totalKills );
okay i have created the object bob and passed some values onto it, using the default construcor. I have my linked list working aswell, and thats all kool. But when i try and add Bob to the linked list it says that its redifining it.

List Players;

Players.insert(Players.begin(),Bob);
okay this is where i have the problem, when i put the above line in it says:

error C2664: 'List::insert' : cannot convert parameter 2 from 'soldier' to 'std::string'
now i dont think i'm declaring the node right, and i dont know if that by placing just the object gavan in the list will also include all of the objects data. Also please note that the link list is working fine, but i'm still learning and have only stored strings in the list, so its all working fine. Nehelp would be much appreciated :)
Advertisement
Looks like your not using std::list, so you'd have to post more information about your list representation to get more help.
The error message says all. It is expecting a string and you are trying to force feed it your soldier. Chances are that:

A. Your parameters to the function are incorrect.

or more likely

B. Your linked list is composed of strings, not soldiers. Linked lists have to be linked lists of specific types so if you did not define that (usually in the declaraion), then there's your problem.
....[size="1"]Brent Gunning
Quote:Original post by DrEvil
Looks like your not using std::list, so you'd have to post more information about your list representation to get more help.


yeah i have created my own list.
/////////This is the declaration inside the list class//////////////////////////////////////// in the header file//void insert(Iterator pos, std::string s);///in list.cpp ///Here is the insert funcctionvoid List::insert(Iterator iter, std::string s){	if(iter.location == NULL)	{		send_back(s);		return;													// everything went ok.	}	Node *after	=	iter.location;	Node *before =	after->backward;	Node *newNode = new Node(s);	newNode->backward	=	before;								// The new Node gets told where to go in the list	newNode->forward	= after;	after->backward = newNode;									// Realign Nodes	if(before ==NULL)	{		first = newNode;	}else	{		before->forward = newNode;	}}



Hope this helps
Look ---

void List::insert(Iterator iter, std::string s)

The second paramater is a string. So why are you trying to send it a soldier?

You have a few options. You could redesign your list object using templates. You could find a way to use strings to represent your soldier. OR, and my favorite, you could use a tried and true list/vector object such as std::vector. You can pick it up in less than a day.
....[size="1"]Brent Gunning
Quote:Original post by skittleo
Look ---

void List::insert(Iterator iter, std::string s)

The second paramater is a string. So why are you trying to send it a soldier?

You have a few options. You could redesign your list object using templates. You could find a way to use strings to represent your soldier. OR, and my favorite, you could use a tried and true list/vector object such as std::vector. You can pick it up in less than a day.




okay thankz man alot, as i said i'm new to link lists and i have only used strings...lol....so.hmzzz....if i pass on a soldier to the list, would that pass over all of its member data aswell?
Becuase what i want to do is have like a ranking system type thing that compares all kills ect and have them in a structured order.

Is that possible or?

thankz very much 4 ur help.
I'd vote for std::list/std::vector also. I'd question your ability to write a custom linked list when a very clear compiler error stumps you. No offense intended.

Your insert/push_back/push_front methods should all take whatever type you are trying to store in the list. This why templates keep these kinds of classes generic, by allowing you to store anything you want.

std::list<soldier> mylist;

soldier Bob(1,"Twinsen","Twhj@jher.com",2345);
mylist.push_back(Bob);
or
mylist.push_front(Bob);
or
mylist.insert(someIterator, Bob);
Quote:Original post by Twinsen2
okay thankz man alot, as i said i'm new to link lists and i have only used strings...lol....so.hmzzz....if i pass on a soldier to the list, would that pass over all of its member data aswell?
Becuase what i want to do is have like a ranking system type thing that compares all kills ect and have them in a structured order.

Is that possible or?

thankz very much 4 ur help.


Glad to help.

Yes, that is possible but what happens when you pass your soldier like that is a duplicate is created so modifying one version but not the other could cause problems.

A better solution is to have a list of pointers to soldiers. This comes with problems in itself, but will at least guarentee that you will not have duplicate soldiers floating around.

Anyway, here is how you would do this with std::vector.

#include <vector>std::vector <soldier*> soldiers;// Create and add a soldiersoldier *newSoldier = new soldier (constructor parameters);soldiers.push_back (newSoldier);// Iterate through the soldiersfor (std::vector <soldier*>::iterator i = soldiers.begin (); i != soldiers.end (); ++i){   soldier *currentSoldier = *i;   // Do what you need to with the current soldier}// Free the memory for the soldiersfor (std::vector <soldier*>::iterator i = soldiers.begin (); i != soldiers.end (); ++i){   delete *i;   (*i) = 0;}soldiers.clear ();

....[size="1"]Brent Gunning
Quote:Original post by DrEvil
I'd vote for std::list/std::vector also. I'd question your ability to write a custom linked list when a very clear compiler error stumps you. No offense intended.

Your insert/push_back/push_front methods should all take whatever type you are trying to store in the list. This why templates keep these kinds of classes generic, by allowing you to store anything you want.

std::list<soldier> mylist;

soldier Bob(1,"Twinsen","Twhj@jher.com",2345);
mylist.push_back(Bob);


yeah soz, i missed that stupid error, yeah, i followed this tutorial thing that i used for reference to create this linked list.
Also for some strange reason i seem 2 be constantly not seing very stupid errors that could be fixed in an instance, and like the more harder stuff i understand.

I have coded an OPenGL tile based game the other day...no worries.
I had an error yesterday that took me two hours 2 find and when i found it i punched myself cause it was so dam stupid.

newayz thankz again 4 ur help all.
No problem man, it's all a learning experience. Didn't mean that as insulting or anything.

This topic is closed to new replies.

Advertisement