0

So I get these errors, and I looked everywhere in the net, tried changing lots of stuff but still stuck...

I have a LinkedList with ListNode and the ListNode has a pointer to an object named Candidate. Candidate has a member which is a pointer to an instance of the object Institute.

When my main ends, then destructor for the linked list is being called, and in the destructor i delete all the nodes. Since delete first calls the destructor of the node, in the Node class in the destructor i do this:

class MyLinkedListNode
{
public:
    Candidate *nodeCand;
    MyLinkedListNode *next;
    MyLinkedListNode *prev;
    ~MyLinkedListNode()
    {
        delete this -> nodeCand;
    }

};

and in Candidate destructor i do this:

Candidate::~Candidate()
{
    delete this -> school ;
}

when school is of type Institute*. (Every new candidate has a new institute)

and:

LinkedList::~LinkedList()
{
    MyLinkedListNode *curr = this -> _head;
    MyLinkedListNode *temp = NULL;
    while(curr != NULL)
    {
        temp = curr;
        curr = curr -> next;
        remove( temp -> nodeCand );
    }
}

and in Institute destructor I do nothing.

Really, I have no idea what I'm doing wrong.

Daniella
  • 71
  • 8
  • Where are your constructors? Also, you don't need `this->`. – Mike DeSimone Jan 01 '14 at 22:21
  • 2
    You're violating the Rule Of Three, most likely. – Kerrek SB Jan 01 '14 at 22:21
  • I dont know the rule of three.. can you explain a bit more? – Daniella Jan 01 '14 at 22:23
  • I would suggest that several Candidates share the same `school` (the pointers point to the same object), so you're trying to delete a single `Institute` object several times. – nullptr Jan 01 '14 at 22:23
  • @Daniella: When you hear a term you don't recognise, _look it up_. – Lightness Races in Orbit Jan 01 '14 at 22:24
  • http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three – yizzlez Jan 01 '14 at 22:24
  • Inspired, every time i create a new Candidate i also create a new Institute... – Daniella Jan 01 '14 at 22:25
  • How do you delete all the list nodes? In the list's destructor, do you read `next` AFTER you delete a node? – nullptr Jan 01 '14 at 22:27
  • After reading the rule of three, I still dont understand what does this have to do with my problem – Daniella Jan 01 '14 at 22:35
  • You need a copy constructor and assignment operator probably. That's how the rule of three (or five) relate to your problem. – RedX Jan 01 '14 at 22:40
  • @Daniella -- Let's go a different route. Does your LinkedList class have a method that deletes a single node? If so, then your destructor should be simple -- just call that "delete a single node" function until the list is empty. – PaulMcKenzie Jan 01 '14 at 22:40
  • That is what i am doing... remove() removes a node and i'm itterating over the list until the node is null. – Daniella Jan 01 '14 at 22:41
  • @Daniella - No. I asked if you already have a function that deletes a single node. If so, and it already works, why are you reinventing your own wheel in the destructor? Just call that working routine in your destructor "n" times. The node you would delete would be the head node each time the loop iterates. If you have a remove() function, it should be taking a parameter, and that would be the node to delete. – PaulMcKenzie Jan 01 '14 at 22:43
  • Sorry, i think thats what i'm doing... – Daniella Jan 01 '14 at 22:46
  • @Daniella - yes, that is what you're doing, but you're doing too much if you have also implemented an "is_empty" function to determine if the list is empty. If so, then that destructor can be written in two lines, and without any traversing of the list. You should post the class interface to your LinkedList class, so that we can see what functions are available. – PaulMcKenzie Jan 01 '14 at 22:51

0 Answers0