I'm trying to overload the assignment operator. The objective is to copy two linked lists.

I copy the first element fine, but for some reason the second loop around I encounter a null pointer and my loop ends on me. My brain is fried from over-studying and I can't think straight. What am I missing here?

LinkedList &LinkedList::operator=(const LinkedList &origList){
	
	if(this != &origList){
		this->~LinkedList();

		NodePointer origPtr, lastPtr;
		origPtr = origList.first;
		lastPtr = new Node(origPtr->data);

		first = lastPtr;

		while(lastPtr != NULL){
			lastPtr = new Node(origPtr->data);
			lastPtr = lastPtr->next;
		}

	}

	return *this; 
}
NodePointer temp;
while(origPtr != NULL)//cycle through origList
  temp = new Node(origPtr->data); //copy data in current node of origList
  lastPtr->next = temp;  //add temp to end of this list
  lastPtr = lastPtr->next; //advance lastPtr to end of this list
  origPtr = origPtr->next; //go to next node in origList

What if the list you are assigning origList to isn't empty?

What if origList is empty?

NodePointer temp;
while(origPtr != NULL)//cycle through origList
  temp = new Node(origPtr->data); //copy data in current node of origList
  lastPtr->next = temp;  //add temp to end of this list
  lastPtr = lastPtr->next; //advance lastPtr to end of this list
  origPtr = origPtr->next; //go to next node in origList

What if the list you are assigning origList to isn't empty?

What if origList is empty?

Well, I'd have to check to see if the list was empty first. If it's empty, I set first = NULL then proceed from there. I'll bring that over from my copy constructor.

What you gave me seems to work, but now I seem to have uncovered another error. Each copy I make has the first element added on twice.

For example, an array of set {1 2 3 4 5} would copy as {1 1 2 3 4 5}. Am I not setting "first" correctly or something? Trying to find it via debug right now.

LinkedList::LinkedList(const LinkedList &origList){
	mySize = origList.mySize;

	if(origList.mySize == 0)
		first = NULL;
	else{
		NodePointer origPtr, lastPtr;

		origPtr = origList.first;
		lastPtr = new Node(origPtr->data);
		first = lastPtr;

		while(lastPtr != NULL){
			lastPtr = new Node(origPtr->data);
			lastPtr = lastPtr->next;
		}
	}
}




LinkedList &LinkedList::operator=(const LinkedList &origList){
	
	if(this != &origList){

		mySize = origList.mySize;

		if(origList.mySize == 0)
			first = NULL;
		else{
			mySize = origList.mySize;
			this->~LinkedList();

			NodePointer origPtr, lastPtr;
			origPtr = origList.first;
			lastPtr = new Node(origPtr->data);

			first = lastPtr;
			NodePointer temp;

			while(origPtr != NULL){//cycle through origList
				temp = new Node(origPtr->data); //copy data in current node of origList
				lastPtr->next = temp;  //add temp to end of this list
				lastPtr = lastPtr->next; //advance lastPtr to end of this list
				origPtr = origPtr->next; //go to next node in origList
			}
		}
	}
	return *this; 
}

Fixed. Dumb error heh, thanks for the help.

check out the below code, that might be helpful:

// Operator overloading in C++
//assignment operator overloading
#include<iostream>
using namespace std;

class Employee
{
private:
    int idNum;
    double salary;
public:
    Employee ( ) {
        idNum = 0, salary = 0.0;
    }

    void setValues (int a, int b);
    void operator= (Employee &emp );

};

void Employee::setValues ( int idN , int sal )
{

    salary = sal; idNum = idN;

}

void Employee::operator = (Employee &emp)  // Assignment operator overloading function
{
    salary = emp.salary;
}

int main ( )
{

    Employee emp1;
    emp1.setValues(10,33);
    Employee emp2;
    emp2 = emp1;        // emp2 is calling object using assignment operator

}
- See more at: http://www.programmingtunes.com/overloading-assignment-operator-in-c/
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.