Check for NULL before accessing any pointer ever, Korhedron. Your lead node should be seg faulting.
Check for NULL before accessing any pointer ever, Korhedron. Your lead node should be seg faulting.
Thanks for all the help ! I've solved that last problem by side-stepping it - checking for NULL, and if so, just skipping the action - which isn't technically correct in the game, but I'll fix that later. Right now, as the debugger keeps pointing out segfaults as I continue, I'm more worried about the fact that the program is starting to look like Swiss cheese. I clearly haven't catered for the exception of a node being the leading or trailing node, so I need a way to cater for it now.
You'll probably recommend std::list again, but knowing these lists will probably change at some point - is there anything else you'd suggest ? Should I write a list class and inherit my Angel and Mortal classes from that ?
Thanks again for all the great help, I'm learning a lot,
Q
Yes, it's true. http://yosefk.com/c++fqa/dtor.html#fqa-11.9There is only but one precious exception to this rule that I have encountered on a regular basis. Using the placement new operator. If you don't know what that even is, then you should not be toying with manual destructors.
But I think we can safely assume that the OP doesn't need to know that at the moment.
[edit]That sounds like a plausible idea. However, it would make more sense to create a list class that can handle Angels and Mortals. Are you familiar with templates? [/edit]Should I write a list class and inherit my Angel and Mortal classes from that ?
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
dwks, I'm not familiar with templates, but I wasn't familiar with linked lists until this project, nor did I understand the importance of pointers. This is the largest C++ app I've written, and I haven't actually written in a few year prior to the last few weeks. So, I'm learning everything as I go.
Would you suggest templates then ? I've looked into inheritance, polymorphism and the like, and understand the concepts, so could probably go about writing a class to manage my lists. It probably wouldn't be much good, but it would at least be less messy :P
This thread is suffering from too many programmers spoiling the top-down reading order. Some of us should stop posting, or go out and design a bulletin board that takes into account when users started typing their posts. Or automatically refreshes the page (in the background, without disturbing your typing) every ten or thirty seconds. Or something.
[edit] See what I mean? Okay.
Maybe you should just make use of what you already know -- templates would suit the problem better, but hey.dwks, I'm not familiar with templates, but I wasn't familiar with linked lists until this project, nor did I understand the importance of pointers. This is the largest C++ app I've written, and I haven't actually written in a few year prior to the last few weeks. So, I'm learning everything as I go.
Would you suggest templates then ? I've looked into inheritance, polymorphism and the like, and understand the concepts, so could probably go about writing a class to manage my lists. It probably wouldn't be much good, but it would at least be less messy :P
Just call the inherited classes AngelList and MortalList or something like that, not just "Angel". It's a collection of Angels. [/edit]
Last edited by dwks; 09-05-2008 at 02:47 PM.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I do agree. I need to get more in the habit of quoting the person I am replying to often because I notice that I frequently slip in between a post I am replying to and another person who is replying to the same thing, but then it looks as if they are questioning me. Or it looks like duplicate information. Or when I end up having someone slip in between me and my intended person I am responding to, I look like I am arguing.
Korhedron, the first person to reply to you mentioned a template that does the linked list functions for you. I highly recommend reading up on pointers a bit before tackling templates, however. If you are having difficulty with pointers, you should understand them before going on to things that are too advanced since they are a staple of the language.
Just to return to this lists class :
All of my problems are arising from me accessing the previous or next pointer on a node that doesn't have a previous or next node. So, technically, all I need is to :
1. Make sure all pointers are NULL when declared
2. Check for NULL before doing any operation that uses these pointers.
I don't see how writing a list object will help. It'll remove about four lines of code in total ( the stuff duplicated in Angel and Mortal classes ), but I'm struggling to see that I can include code to manage my pointers inside the object itself. The objects are what makes the list, and the operations on pointers are done "between objects", not from inside an object... In this way, I'm very confused.
There are TWO advantages of doing a list class:
1. You separate the list handling from your other code - so you can work on making the list class work correctly separately from making other code work correctly [you have a list component that is "known good", so you don't need to worry about it].
2. It teaches you how to use inheritance to achieve more complex stuff. This is of course a completely non-technical reason - but an equally good one when you are learning. [And if the purpose here wasn't to learn, then we'd be back at recommendin std::list].
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Mats, thanks again. Though, I'm still struggling to imagine placing the list management code INSIDE the objects themselves. I can put in my next and prev pointers, but the code from going from one node to the other, and the code for deleting a node - that can't fit inside the actual object, so that code won't be a "known good", it'll just be exactly the same as I currently have.
Or am I entirely deluded ? :P
Yeah, pretty much. Plus never calling destructors, but that's probably not something you'll do once you know you shouldn't.
Some operations, like deleting a list item, you simply can't really do from "inside" one of the list objects. You do sort of need a controller class or function or whatever.I don't see how writing a list object will help. It'll remove about four lines of code in total ( the stuff duplicated in Angel and Mortal classes ), but I'm struggling to see that I can include code to manage my pointers inside the object itself. The objects are what makes the list, and the operations on pointers are done "between objects", not from inside an object... In this way, I'm very confused.
But I think you're right, a list object wouldn't help much in this case -- because it's a linked list. You can't really have a AngelList::delete() method for linked lists.
Deleting objects is the hard part, I think. You have to keep in mind the fact that the root object could change. (So maybe returning a new root from the delete() function or whatever would be a good idea.) The list could become completely empty.
One mistake I consistently made when I was creating my first linked lists, something that you're doing too, was assume that the linked list always had at least one object. It just confuses everything. Write your code carefully, so that the root object can be NULL.
[edit] Beaten again -- by two posts! What is it with this thread?
That's easy -- just call traverse() or whatever in the next node.but the code from going from one node to the other
Yeah, that's a bit tougher. It would probably be easier to have an AngelList to do that . . . .and the code for deleting a node - that can't fit inside the actual object
[/edit]
Last edited by dwks; 09-05-2008 at 03:31 PM.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Something like this (actually, the Angel and Mortal are nodes, not lists - because the list is a separate thing.
--Code:class Node { public: Node *next; Node *prev; Node() : next(0), prev(0) {}; }; class List { private: Node *first; Node *last; public: List() : first(0), last(0); void Insert(Node &); void Remove(Node &); }; class Angel : public Node { ... your angel definition goes here ... }; class Mortal : public Node { ... } List AngelList; List MortalList;
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Also, doing this gives you a natural progression to learning templates, because the problems you'll encounter working with this code are the main motivation behind templates.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law