I'm trying to write a program that uses the stack ADT with a linked list implementation. I can't seem to overload the assignment operator. I keep getting an error that says
"StackClass::Pop cannot convert 'this' pointer from 'const StackClass' to 'StackClass&'"
All of my other methods are working correctly, but let me know if you need more
The assignment doesn't look right. When one uses assignment, one doesn't expect the right-hand operator to be modified, yet here it's being emptied.
What you should do is access the members yourself, instead of relying on the methods.
How is the copy constructor implemented? The assignment should look practically the same.
In fact, lately I've been implementing the two like this:
1 2 3 4 5 6 7 8 9 10 11 12
class A{
//members
public:
A::A(const A &b){
//initializations
(*this)=b;
}
A &operator=(const A &b){
//delete all dynamically allocated members
//perform assignments
}
};
This is the entire .cpp and .h files, this is the one that give problems because I'm trying to modify a const in the operator=, I know I can't modify orig, but I really can't think of what to do. I either end up with the previous error message, or I get one about infinite recursion.
helios - when I originally learned c++, I was told that *this returns a shallow copy so my copy constructor isn't going to do what I want it to.
when I originally learned c++, I was told that *this returns a shallow copy so my copy constructor isn't going to do what I want it to.
No. this is a pointer to the object that calls the member. Therefore, *this is the object itself. Doing *this=b is no different from doing *pointer=object. In my example, line 6 is calling A::operator=(), so it's equivalent to what you did on line 61.
If that's how your copy constructor was implemented when you posted