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");
}
ListOfEmp::ListOfEmp(const ListOfEmp &e) {
*this = e;
}
Bases the copy constructor around the assignment operator. Unfortunately, the assignment operator
const ListOfEmp& ListOfEmp::operator=(const ListOfEmp e){
...
}
takes the ListOfEmp
to be assigned by value, invoking the copy constructor which invokes the assignment operator which invokes the copy constructor which invokes the assignment operator which invokes the copy constructor which invokes the assignment operator which invokes the copy constructor....
Uncontrolled recursion.
The solutions are pass by reference
const ListOfEmp& ListOfEmp::operator=(const ListOfEmp & e){
...
}
and rewriting the other way around, Assignment operator based on the copy constructor, to take advantage of the ever-popular Copy and Swap Idiom.
If you go with the first option, note the assignment operator is overly complicated and has a logic error
if (head = NULL) // Whoops. Annihilated head! Should be head == NULL
and the incomplete code leaves plenty of room for other errors in code that has not been provided.
scohe001 also correctly notes that head
is not being initialized in the copy constructor. This is more likely than not the mistake that is triggering the crash.
After reading the OP's question and a decently provided answer worth a thumbs up for their help, but still is lacking in some answering only due to the OPs lack of submitting a Minimal, Complete, & Verifiable example I tend to think that the OP is a victim of:
As well as any other unseen issues or language specific grammatical errors that can not be seen by the community from source that wasn't submitted.
User contributions licensed under CC BY-SA 3.0