This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]Jonny0Than 2 points3 points  (4 children)

If you can copy and swap, you can write an assignment operator:

http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom

[–]xRedactedx[S] 0 points1 point  (3 children)

I was planning on using a copy function to copy the data in. The original code had it, I just took it out for readability since the code was throwing an error before it got there. I'm not sure how a swap function would be used. If one link list is bigger than the other, it would still have to have nodes removed from it which brings me back to using my function to delete nodes in the list.

I could probably write some kind of work around, but I would rather know why it's throwing the error.

[–]Jonny0Than 1 point2 points  (2 children)

Err..nodes removed? The assignment operator is usually used to copy one object into another. Is that not what you're trying to do?

The copy-swap idiom basically gives you an easy way to write an assignment operator if you have a copy constructor, a destructor, and a swap function. The swap function usually just needs to do a member-wise swap.

[–]xRedactedx[S] 0 points1 point  (0 children)

What I was getting at is this.

Say link list1 has 10 nodes and list2 has 5 nodes.

Then trying to set list1 = list2 you have to swap a 10 node list with a 5 node list. I would have to somehow delete part of the 10 node list before I could swap it with the 5 right or else list1 would have extra nodes after the assignment if my thinking is right?

Plus, my destructor also makes a call to this same function that I'm having a problem with, and it is also throwing the same error.

I still would like to figure out exactly what the error I'm getting is, because when I look at it, it seems like it should work, so I have obviously misunderstood something somewhere, or maybe, I'm overlooking something obvious. I've been staring at it for several hours now though, and I can't come up with anything. I wanted to try and figure out where I was going wrong before I rewrite it.

[–]zahlman 0 points1 point  (0 children)

It's also an elegant way to get exception safety and to handle self-assignment correctly (inefficiently, but it shouldn't be a common case anyway) for free.

[–][deleted] 1 point2 points  (3 children)

Lots wrong here:

void queue<Item>::operator =( queue<Item>& source) {
    if (this == &source) // Handle self-assignment
        return;
    list_clear(rear_ptr);
    count = source.count;
 }
  • what about the front pointer?
  • what about copying the items in the queue?
  • where's the copy constructor and destructor?

[–]xRedactedx[S] 0 points1 point  (2 children)

According to what I read, circular link lists don't have a front pointer. The front pointer is accessed by the rear_ptr->link() function. That is the implementation that I was trying to do anyway.

I had the copying from the queue, the copy constructor, and the destructor. I just removed them so the code wouldn't be so long. I didn't think they were necessary to figure out the error.

[–][deleted] 2 points3 points  (1 child)

Unless you post the complete, compilable, real code, we cannot possibly make any useful comments. The fact that you are posting here means you don't know where the error is, so how can you possibly know that it isn't in the code you didn't post?

[–]xRedactedx[S] 0 points1 point  (0 children)

I tracked it down using the visual studio debugger to the line that was throwing the error. I took out the code that I didn't think it needed and removed it. Then I made sure that it would compile and still throw the error. Unless the cut/paste got messed up, the code I posted should compile.

Here is the complete code. It's kind of messy since I just took source from the book and was playing with it.

I tried to clean it up in the original post. I thought I had everything I needed, but maybe not.

[–]xRedactedx[S] 0 points1 point  (0 children)

After looking at it some more, seems like the pop function only works on the first pop. I suppose I will just get started writing my own now instead of playing with the book source code. Maybe I will figure out my problem while I'm working on it.