hi guys,

hopefully somebody can tell me what i'm doing wrong here:

i'm trying to put together a copy constructor with this linked list class, but i just cannot get it to compile:

#ifndef LINKEDLIST_H
#define LINKEDLIST_H

//*************Include Statements*************
using namespace std;	// for cout
//***********END Include Statements***********

class NumberList
{
	private:
		class ListNode
		{
			friend class NumberList;
			double value;
			ListNode *next;
			ListNode(double value1, ListNode *next1 = NULL)
			{
				value = value1;
				next = next1;
			}
		};
		ListNode *head;				// List head pointer
	public:
		NumberList(){head = NULL;}	// constructor
		NumberList(NumberList  &);


		~NumberList();
		void appendNode(double);
		void insertNode(double);
		void deleteNode(double);
		void displayList();

};

 NumberList::NumberList(NumberList &obj)
 {
	 ListNode *copyList, *oldList;
	 double value;
	 oldList = &obj;
	 copyList = obj.head;
	 value = copyList->value;
	 head = new ListNode(value);
	 
	 while (copyList->next != NULL)
	 {
		 copyList = obj.head->next;
		 value = copyList->value;
		 copyList = new ListNode(value, head);
	 }
 }


NumberList::~NumberList()
{
	ListNode *nodePtr, *nextNodePtr;

	nodePtr = head;
	while (nodePtr != NULL)
	{
		nextNodePtr = nodePtr->next;
		delete nodePtr;
		nodePtr = nextNodePtr;
	}
}

void NumberList::appendNode(double num)
{
	if (head == NULL)
		head = new ListNode(num);
	else
	{
		// nonempty list
		ListNode *nodePtr;	// used to traverse the list
		nodePtr = head;
		// traverse list to locate last node
		while (nodePtr->next != NULL)
			nodePtr = nodePtr->next;
		// ptr->next is NULL so nodePtr points to last node
		// create new node and make it successor of what
		// is now the last node
		nodePtr->next = new ListNode(num);
	}
}


void NumberList::insertNode(double num)
{
	ListNode *nodePtr, *previousNodePtr;
		if (head == NULL || head->value >= num)
	{
		// new node goes at beginning
		head = new ListNode(num, head);
	}
	else
	{
		previousNodePtr = head;
		nodePtr = head->next;
		// find the insertion point
		while (nodePtr != NULL && nodePtr->value < num)
		{
			previousNodePtr = nodePtr;
			nodePtr = nodePtr->next;
		}
		// create the new node and insert it
		// just before nodePtr
		previousNodePtr->next = new ListNode(num, nodePtr);
	}
}

void NumberList::deleteNode(double num)
{
	ListNode *nodePtr, *previousNodePtr;
	
	// If the list is empty, do nothing.
	if(!head)
		return;
	//Determine if the first node is the one to delete.
	if (head->value == num)
	{
		nodePtr = head;
		head = head->next;
		delete nodePtr;
	}
	else
	{
		//Initialize nodePtr to head of list
		nodePtr = head;

		// Skip all nodes whose value member is 
		// not equal to num.
		while(nodePtr != NULL && nodePtr->value != num)
		{
			previousNodePtr = nodePtr;
			nodePtr = nodePtr->next;
		}
		// Link the previous node to the node after
		// nodePtr, then delete nodePtr.
		if (nodePtr) 
		{
			previousNodePtr->next = nodePtr->next;
			delete nodePtr;
		}
	}
}


void NumberList::displayList()
{
	ListNode *nodePtr;
	nodePtr = head;
	while (nodePtr)
	{
		cout << nodePtr->value << endl;
		nodePtr = nodePtr->next;
	}
}

#endif

can anyone offer a hand as to why the copy constructor will not work? thanks!

You're trying to assign a pointer to an object of type NumberList to a pointer to an object of type ListNode.

EDIT: Though I'd thought that would be explicitly specified in the compiler error?

Other code defects:
1. Right copy constructor signature is:

NumberList::NumberList(const NumberList&);

2. What happens if you want to initialize new list with empty list? The right answer: memory access failure occured. No obj.head == 0 test in copy constructor.
3. You must define overloaded operator=() or make it inaccessible (private) for the class NumberList. Remember: if you need non-default copy constructror then you need your own assignment operator too (and vice versa).
...
Next time use code tag with the language specifier:
[code=cplusplus] source

[/code]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.