Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It's because you took your List by value- that is, you copied it. You need to take it by const reference- that is,</p> <p><code>ostream &amp;operator&lt;&lt;(ostream &amp;stream, const List&amp; theList)</code></p> <p>Furthermore, your assignment operator should take a const reference, not a non-const ref.</p> <p>I also don't think that this</p> <p><code>delete _head, _tail, _current, _del;</code></p> <p>does what you think it does. It just deletes <code>_del</code> and none of the others.</p> <p>You should use a recursively self-owning design, where each node deletes the next one in the chain. Edit: As this was unclear, I'm going to post a brief sample.</p> <pre><code>template&lt;typename T&gt; struct Node { Node(const T&amp; arg, Node* prev) : t(arg), previous(prev) {} T t; std::auto_ptr&lt;Node&lt;T&gt;&gt; next; Node&lt;T&gt;* previous; }; template&lt;typename T&gt; class List { std::auto_ptr&lt;Node&lt;T&gt;&gt; head; Node&lt;T&gt;* tail; public: List() : head(NULL), tail(NULL) {} void clear() { head = tail = NULL; } // Notice how simple this is void push_back() { push_back(T()); } void push_back(const T&amp; t) { if (tail) { tail = (tail-&gt;next = new Node&lt;T&gt;(t, tail)).get(); return; } tail = (head = new Node&lt;T&gt;(t, NULL)).get(); } void pop_back() { tail = tail-&gt;previous; tail-&gt;next = NULL; } void pop_front() { head = head-&gt;next; } List&lt;T&gt;&amp; operator=(const List&amp; ref) { clear(); // Clear the existing list // Copy. Gonna leave this part to you. } }; </code></pre> <p>Notice how I never deleted anything- <code>std::auto_ptr</code> did everything for me, and manipulating the list and it's nodes became much easier, because <code>std::auto_ptr</code> manages all of the memory involved. This list doesn't even need a custom destructor.</p> <p>I suggest that you read up heavily on <code>std::auto_ptr</code>, it has some surprises in it's interface, like the "copy" constructor and assignment operator actually move, that you are going to want to know about before using the class. I wish I could start using <code>std::unique_ptr</code> instead all of the time, which would make life so much easier.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload