Im trying to make a copy of an object, which is a circular queue. My Enqueue and Dequeue work correctly, but whenever I do this, I get a runtime error.
CQUEUE j = k;
The output window says that my copy constructor is recursive on all control paths? Can someone help me figure out what I am doing wrong please? Here is my copy constructor, along with the overloaded assignment operator.
CQUEUE::CQUEUE(const CQUEUE& original)
{
(*this) = original;
}
void CQUEUE::operator=(CQUEUE w)
{
qnode *p = w.front;
(*this).front = new qnode;
(*this).back = front;
while(p -> next != w.back)
{
back -> num = p -> num;
p = p -> next;
back -> next = new qnode;
back = back -> next;
}
back -> num = p -> num;
p = p -> next;
back -> next = new qnode;
back = back -> next;
back -> num = p -> num;
back -> next = front;
front -> prev = back;
}
The compiler is making this line call the copy constructor instead of your = operator, because your = operator currently takes a non-const, by-value CQUEUE. Instead, your = operator should look like this:
Then your copy constructor will call your operator = instead. Alternatively, make it more explicit:
EDIT: Yes, you’re correct, you still do need a copy constructor.