If you're assigning to yourself (this == &rightSide), then line 4 clears the list.
In fact, line 4 clears the list either way, and it leaks the items that were there. Really, the very first line should be if (this != &rightSide) {
You'll crash at line 10 if rightSide is empty (dereferencing the null rightSide->first pointer)
Do you have a method that appends an item to the end of the list? If not, then there's no reason for a lastPtr member. If you do, then you can greatly simplify this code:
im trying to create a overloaded assignment operator of a linked list with this struct
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
struct item
{
string name;
double price;
int sold;
int stock;
};
…
const List& List::operator=(const List& rightSide)
{
...
first = 0;
...
first = new Node(rightSide.first->data); // copy first node
...
}
the definition works with ints but gives me errors for the struct
Could you please add details about your code?
I assume “first” is a pointer to Node-type, but what is the relationship between your “item” and your “Node”?