• Advertisement
Sign in to follow this  

Storing Objects in a Linked List

This topic is 4590 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 :)

Share this post


Link to post
Share on other sites
Advertisement
Looks like your not using std::list, so you'd have to post more information about your list representation to get more help.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 funcction

void 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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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 soldier
soldier *newSoldier = new soldier (constructor parameters);
soldiers.push_back (newSoldier);

// Iterate through the soldiers
for (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 soldiers
for (std::vector <soldier*>::iterator i = soldiers.begin (); i != soldiers.end (); ++i)
{
delete *i;
(*i) = 0;
}
soldiers.clear ();


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
No problem man, it's all a learning experience. Didn't mean that as insulting or anything.

Share this post


Link to post
Share on other sites
Quote:
Original post by DrEvil
No problem man, it's all a learning experience. Didn't mean that as insulting or anything.


man, none taken, this site is really great.
The people here are always willing to help and are really friendly.
Umm yeah...I posted this like 20 mins ago and already like 8 posts and 60 views, which is tops.

Yeah it is all a learning experience..lol...just these stupid simple errors i keep fumbling on.

Thankz once again every1 4 ur help :)

Share this post


Link to post
Share on other sites
Just to add to the learning experience. Drop the AOL speak. "every1" is not a word. Neither is "4" a replacement for good old "for" nor "ur" for "your". This is a forum, not some kiddie chat room.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement