Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking Copy Constructor w/Linked Lists
    text
    copied!<p>This portion of my current assignment has me completely stumped: </p> <p>I am trying to build a LinkedList class that includes a copy constructor, which itself calls a static method (*makeCopy()). I cannot figure out how to make the makeCopy method work (all method headers were given to us in the assignment). </p> <pre><code>class ListNode { public: ListNode(double v, ListNode *p) { value = v; next = p; } private: double value; ListNode *next; friend class LinkedList; // LinkedList has friend status }; // The linked list class itself class LinkedList { public: //Add number to Linked List void add(double x) { if (head == NULL) { head = new ListNode(x, NULL); return; } else { ListNode *nodePtr = head; head = new ListNode(x, nodePtr); } } LinkedList() { head = NULL; } //Default destructor ~LinkedList() { ListNode *nodePtr = head; while (nodePtr != NULL) { ListNode *garbage = nodePtr; nodePtr = nodePtr-&gt;next; delete garbage; } } //Copy Constructor LinkedList(LinkedList &amp;); //Copy function static ListNode *makeCopy(ListNode *pList); private: ListNode * head; }; </code></pre> <p>Below is how I am trying to write the makeCopy function:</p> <pre><code>LinkedList::LinkedList(LinkedList &amp;list) { *makeCopy(list.head); } static ListNode *makeCopy(ListNode *pList) { if(pList-&gt;value == NULL) { return NULL; } else { ListNode *node = pList; while(node != NULL) { ListNode newOne = *node; node = node-&gt;next; } } return; } </code></pre> <p>First off I continue to get the error "member ListNode::value is innaccesible." Second I'm very new to C++ and pretty sure I'm way off. If someone could help nudge me in the right direction it would be greatly appreciated. Thx</p> <p> This is my current iteration of the copy constructor and the makeCopy method. Unfortunately the ListNode constructor, along with all the method headers, are part of the assignment. We have to write the code for them. The error I'm getting now is this: both return statements say "Error: a constructor or destructor may not return a value." I'm completely lost on this one since this is just a return statement and I don't see either a constructor or destructor. </p> <pre><code>LinkedList::LinkedList(LinkedList &amp;list) { LinkedList *copy = new LinkedList(); while(list.head != NULL) { ListNode node = *makeCopy(list.head); copy-&gt;add(node.value); list.head = head-&gt;next; } return *copy; } ListNode * LinkedList::makeCopy(ListNode *pList) { ListNode node = *pList; return *node; } </code></pre>
 

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