I'm having some issues with the Assignment Operator. Although there are no red underlining errors, when I compile the program would break at
emp1 = emp2;
in main.cpp, everything was working until I added the assignment operation function.
Any Advice will be greatly appreciated.
Edit: Just thought I should show specific code that relates to the issue instead of the whole thing.
Here's what I've written:
ListOfEmp.h
public:
ListOfEmp();
ListOfEmp(const ListOfEmp &);
~ListOfEmp();
const ListOfEmp& operator=(const ListOfEmp e);
};
ListOfEmp.cpp
ListOfEmp::ListOfEmp():head(NULL)
{
}
ListOfEmp::ListOfEmp(const ListOfEmp &e) {
*this = e;
}
ListOfEmp::~ListOfEmp()
{
clear();
}
const ListOfEmp& ListOfEmp::operator=(const ListOfEmp e){
if (this != &e) {
clear();
EmpNode* copy = NULL;
EmpNode* orig = e.head;
while (orig != NULL) {
if (head = NULL) {
head = copy = new EmpNode((orig->emp).name, (orig->emp).salary);
}
else {
copy->next = new EmpNode((orig->emp).name, (orig->emp).salary);
copy = copy->next;
}
orig = orig->next;
}
}
return *this;
}
void ListOfEmp::clear() {
EmpNode* temp = head;
while (temp != NULL) {
temp = temp->next;
delete head;
head = temp;
}
}
Main.cpp
int main() {
ListOfEmp emp1;
emp1.insertAtfront("John", 20000.00);
emp1.insertAtfront("Dave", 24500.50);
emp1.insertAtfront("Joshua", 33567.60);
emp1.deleteMostRecent();
emp1.getSalary("Dave");
cout << endl;
cout << emp1;
cout << endl;
ListOfEmp emp2;
emp2.insertAtfront("Julio", 54000.00);
emp2.insertAtfront("Mike", 12000.00);
emp2.getSalary("Mike");
cout << endl;
cout << emp2;
cout << endl;
emp1 = emp2;
cout << emp1;
cout << endl;
cout << emp2;
system("pause");
}