• Advertisement
Sign in to follow this  

Elementary Linked List crashing (C++) [RESOLVED]

This topic is 3761 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

Hi all, The addFace(..) method causes this simple linked list to crash. I haven't a clue why, but I've narrowed it down to line 21: (*last).next = &f; (to clarify, this compiles, but crashes at runtime) #include <iostream> using namespace std; class Face {}; class FaceNode { public: Face f; FaceNode *next; FaceNode() {}; FaceNode(Face _f) { f = _f; } }; class FaceList { public: FaceNode *first, *last; void add(FaceNode f) { //make the pointer in the current tail point to the new node (*last).next = &f; //---CRASHES HERE------------ //now the tail points to the new facenode last = &f; } FaceNode getFirst() { return *first; } }; class Model { public: FaceList faces; void addFace(Face f) { FaceNode fn(f); faces.add(fn); } }; int main() { Model m; Face face; m.addFace(face); // causes program to crash cout << "hello, world" << endl; system("PAUSE"); return 0; } Any ideas of why this is happening? Thanks in advance [Edited by - MattHughes on November 4, 2007 10:23:32 PM]

Share this post


Link to post
Share on other sites
Advertisement
Where do you initialize the value of last? You cant do (*last).next if last is null.

Share this post


Link to post
Share on other sites
You know, there's this awesome thing in the standard library called std::list...

Share this post


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

  • Advertisement