0

I am trying to print the data of a node from a double linked list using all shared_ptr, but outputting the data with the print function causes the program to crash. I don't know why it crashes when I try to print it. Could there be something wrong with my Insert function?

template <typename T>
class Node
{
   private:
      T data;
      shared_ptr<Node<T>> next;
      shared_ptr<Node<T>> prev;

   public:
      Node() { next = NULL; prev = NULL; data = 0; }
      Node(T value) { next = NULL; prev = NULL; data = value; }
      T getData() { return data; }
      shared_ptr<Node<T>> getNext() { return next; }
      shared_ptr<Node<T>> getPrev() { return prev; }

};

template <typename T>
class DoubleLinkedList
{
   private:
      shared_ptr<Node<T>> head;
      shared_ptr<Node<T>> tail;
   public:
      DoubleLinkedList()
      {
         head = NULL;
         tail = NULL;
         total = 100;
         for (int i = 0; i < total; i++)
         {
            Insert(objectgetsinsertedhere);
         }
      }

      void print()
      {
         shared_ptr<Node<T>> temp;
         temp = tail;
         temp = temp->getPrev();
         cout << temp->getData().getName();  // getName() is a getter to get the name of the object
      }


      void Insert(T data)
      {
         T value(data);

         if (head == NULL)
         {
            head = make_shared<Node<T>>(data);
            tail = head;
         }
         else
         {
            shared_ptr<Node<T>> nu = make_shared<Node<T>>(data);
            nu->getPrev() = tail;
            if (head == tail)
               head->getNext() = nu;
            tail->getNext() = nu;
            tail = nu;
         }
      }
};
1
  • You can always use a debugger. Commented Apr 21, 2016 at 6:46

2 Answers 2

1

Your assignments in Insert are not doing what you want. These calls:

nu->getPrev() = tail;
if (head == tail)
    head->getNext() = nu;
tail->getNext() = nu;

are all returning a copy the the content of the next and prev fields. The accessors (get/set) create a new shared_ptr object and initialize them with the current value of the prev and next fields. When you reassign the content of these copied shared_ptrs you don't manipulate the state of the node.

So you should either operate on the plain fields (prev and next), return a reference to the fields in the accessor (like shared_ptr>& getNext() { return next; }) or add setter methods for the fields and use those.

Sign up to request clarification or add additional context in comments.

4 Comments

I've set them as shared_ptr<Node<T>>& getNext() { return next; } and the other getter for prev to return a reference, but it still crashes. Would I have to fix something else as well?
Also, if I were to operate on the plain fields prev and next, would I need to make class Node a friend for DoubleLinkedList?
Yes, you need to make it a friend. The tail->getNext() = nu assignment also looks wrong in general, because tail should never have a next - maybe that's the other problem - but I leave that now to you to figure it out.
Okay got it. I've declared friend class Node<T>; inside the DoubleLinkedList class but I'm getting errors saying the prev and `next are private when I operate on the plain fields. Am I declaring it wrong?
0

Do you have a copy constructor defined in your DoubleLinkedList class?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.