I'm adding a copy constructor and overloaded assignment operator to a linked list header file and implementation file, but I am getting notes and errors from the compiler that I do not understand. Here's the .h file:
#include <cstdlib>
class NumberList
{
private:
int size; // Easier for copy constructor and assignment operator
// Declare a structure for the list
struct ListNode
{
double value; // The value in this node
struct ListNode *next; // To point to the next node
};
ListNode *head; // List head pointer
ListNode *first = head; // Create first to use in functions without messing with head
public:
// Constructor
NumberList()
{ head = NULL; }
// Copy Constructor
NumberList(const NumberList &);
// Assignment Operator
NumberList& operator = (const NumberList&);
// Destructor
~NumberList();
// Linked list operations
void appendNode(double);
void insertNode(double);
void deleteNode(double);
void displayList() const;
bool isEmpty(ListNode*);
};
NumberList::NumberList(const NumberList &obj)
{
// Copy list's size into function for use
int mySize = obj.size;
// If list's size is 0 create new list with head as null
if (obj.size == 0) {
head = NULL;
}
else {
// Do it again to ensure then destruct the list
mySize = obj.size;
this->~NumberList();
// Define nodes for use in function
ListNode *firstPtr, *lastPtr;
// Define firstPtr's position at top then move obj's first to a new pointer
firstPtr = obj.first;
lastPtr = new ListNode(firstPtr->value);
first = lastPtr;
// Traverse the loop while copying it's contents
while (lastPtr != NULL) {
lastPtr = new ListNode(firstPtr->value);
lastPtr = lastPtr->next;
}
}
}
NumberList& NumberList::operator = (const NumberList &obj)
{
if (this != &obj) {
this->~NumberList();
// Copy list's size into function for use
int mySize = obj.size;
// If list's size is 0 create new list with head as null
if (obj.size == 0) {
head = NULL;
}
else {
// Do it again to ensure then destruct the list
mySize = obj.size;
this->~NumberList();
// Define nodes for use in function
ListNode *firstPtr, *lastPtr;
// Define firstPtr's position at top then move obj's first to a new pointer
firstPtr = obj.first;
lastPtr = new ListNode(firstPtr->value);
ListNode *temp;
// Traverse obj
while (firstPtr != NULL) {
// Copy current node's data
temp = new ListNode(firstPtr->value);
// Temp goes to the end of the list
lastPtr->next = temp;
// lastPtr goes to the end of the list
lastPtr = lastPtr->next;
// Go to next node
firstPtr = firstPtr->next;
}
}
}
return *this;
}
And the errors:
1 2 3 4 5 6 7 8 9 10 11 12
NumberList.cpp:243:47: error: no matching function for call to ‘NumberList::ListNode::ListNode(double&)’
lastPtr = new ListNode(firstPtr->value);
^
NumberList.cpp:243:47: note: candidates are:
In file included from NumberList.cpp:4:0:
NumberList.h:13:11: note: NumberList::ListNode::ListNode()
struct ListNode
^
NumberList.h:13:11: note: candidate expects 0 arguments, 1 provided
NumberList.h:13:11: note: NumberList::ListNode::ListNode(const NumberList::ListNode&)
NumberList.h:13:11: note: no known conversion for argument 1 from ‘double’ to ‘const NumberList::ListNode&’
NumberList.cpp:248:51: error: no matching function for call to ‘NumberList::ListNode::ListNode(double&)’
It's this error and notes repeated at lines 243, 248, 284, and 291. I know it has something to do with how I'm creating these new nodes but this is the most I could slim those errors down and I have no clue what the response here is. Checking online most people have a "NodePointer" there instead of a node, but I don't know the proper way to define that in the header.
the compiler can't read your mind.
you haven't defined a constructor for ListNode, ¿what should happen when you do new ListNode(42);?
either define such constructor or stop trying to call it.
> most people have a "NodePointer" there instead of a node, but I don't know
> the proper way to define that in the header.
¿? you do have a "node pointer" there.
> this->~NumberList();
suicide is not the answer
if you want to run clean up code, then create a clean_up() function