# Linked Lists with classes not structs?

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

## Recommended Posts

Thanks to you guys earlier, I was able to figure out my problem and it seems to be working perfectly. Now I have run into a second delimma. I have been successfully utilizing a linked list for my animation sequences in the game with no flaws at all. I planned to use this same attack to hold all of the character classes in the game so I could easily iterate through them and render, detect collision, etc. The difference between my animation sequences and my characters is the animations are in a structure and the characters are all classes. I attempted to preform the linking the same way I did with the structures and I get an access denied error every time I try to loop through the objects even though it's set up exactly like the other linked list thats working perfectly. Am I wrong thinking that you can not link classes together as you can structures? I have a feeling that's an incredibly wrong assumption. Here is a small tidbit of code to shed some light on my problem. This source shows how I am attempting to loop through the classes.
	CCharacter* Last_Added_Character = ObjCollection->GetLastCharacter();

}


The ObjCollection class is the class that preforms loading of characters and sets the "Next" pointer within each CCharacter class as well as keeps track of the last loaded character to define the endpoint/startpoint of the "list". Here's some source of how I am using and setting the "Next" pointer within the ObjCollection.
	CCharacter* New_Character = new CCharacter(myD3DDevice);

//Load all character information from a text file
//Stuff that is irrelevant to this postttt

New_Character->Next = Last_Object;
Last_Object = New_Character;

Hope this all makes sense and I apologize for asking so many odd questions today. Thanks for all your help everyone!


##### Share on other sites
Language?

Assuming c++, then your code *should* work equally well (or badly =P ) for both classes and structs. Any particular reason we're not using std::list ?

Quote:

Can you elaborate? I assume you mean a segmentation fault...

##### Share on other sites
To elaborate, at run time when it gets to any of those three lines within' the while loop it runtimes and when I exit the game it has a popup box that says Unhandled Exception: 0xC0000005: Access Violation. It makes me think that the class is is referencing does not exist. Yes, I am using C++. I actually very new to the concept of linked lists, thats why I havent attempted std::list.

##### Share on other sites
Quote:
 Original post by jrmiller84To elaborate, at run time when it gets to any of those three lines within' the while loop it runtimes and when I exit the game it has a popup box that says Unhandled Exception: 0xC0000005: Access Violation. It makes me think that the class is is referencing does not exist. Yes, I am using C++. I actually very new to the concept of linked lists, thats why I havent attempted std::list.

|re you doing this to learn about how to make linked lists, if so post your code and i will give you pointers as to what may be the problem. OTOH, it is important that you do as much this yourself as you can so that you fully understand what is going on ( and what is going wrong ) in your code.

If you only want to use linked lists, then you would be better off using std::list, you will have less obscure bugs that way.

##### Share on other sites
No I understand the concept behind linked lists in the way I am currently doing it based off of some tutorials I have read. I do not know how to do it using std::list. If someone wants to point me in the direction of a good tutorial for using them then I will surely read it and see if it will work in my code. Otherwise I am just looking for insight onto my particular problem as I am already familiar with its workings.

##### Share on other sites
Quote:
 Original post by jrmiller84No I understand the concept behind linked lists in the way I am currently doing it based off of some tutorials I have read. I do not know how to do it using std::list. If someone wants to point me in the direction of a good tutorial for using them then I will surely read it and see if it will work in my code. Otherwise I am just looking for insight onto my particular problem as I am already familiar with its workings.

Concepts are one thing. Implementation in code can be quite a bit away from that. Subtle bugs may be passing under your notice which affect how the list works. Either you want to debug your current implementation, or you want to use std::list.

I use this as my STL reference(its easy to remember, its the first result google returns for std::vector ). It may not be suitable for people unfamiliar with the STL, but I have no links of that nature to hand.

Here is how you would implement you code using std::list
	std::list<CCharacter> objCollection;	for( std::list<CCharacter>::iterator it = objCollection.begin() ; it != objCollection.end() ; ++it ) {		it->IncrementFrameCt();		PrimitiveInterface->SetVertexAndRender(*it);	}// ..................................        objCollection.push_front( CCharacter(myD3DDevice) );

Or use std::list<CCharacter*> if that is required for whatever reason( inheritance, etc ).

##### Share on other sites
oh wow, that actually helped quite a bit. I rewrote a small segment of code to compensate for std::list and it worked perfectly on the first compile. I guess I was a little intimidated by it, I didn't understand how the lists were defined since they look quite a bit different than other declarations. Do you guys think this is a better way of doing things, for example, do you think it would be beneficial to go back and rewrite my animation sequences to use this new way of iteration instead of the way I was doing it? I'm guessing that may be a yes.

##### Share on other sites
The answer is a definate yes. Not only will it be more thoroughly tested than your linked list, exception safe and highly optimized, the implementation provided with your compiler will be tweaked to work with optimisations specific to your compiler for even better performance.

##### Share on other sites
gets the right 'first' pointer -- to the first 'Character'.

New_Character-)Next = Last_Object;
Last_Object = New_Character;

looks like its adding a new top to an 'Object' chain instead of a 'Character' chain........

When you cloned it, did you substitute all the right stuff ????

##### Share on other sites
Not to jump off-topic, but joanusdmentia, nice icon and sig. Amazing game and great movie. I don't know why that game got such low ratings, I loved it. Back on topic, it does only make sense I utilize it. Thanks for the input on that! Now that I've broken through this barrier with all your help, I am onto other much simpler tasks, thank goodness.

As for anon's post I believe I did pretty much copy everything over. I guess it's kind of irrelevant now that I have the std::list working correctly. You guys are always an awesome help. Thanks a lot everyone and sorry for so many odd lengthy questions. As you can tell, I'm learning this stuff as I go.

• 40
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631371
• Total Posts
2999605
×