1

getNode():

    Node<type> getNode(int position) {
        Node<type> *item = head;
        for (int i = 0; i < position; ++i) {
            item = item->next;
        }
        return *item;
    };

Node Swapping Code:

        Node<Guitar*> temp;
        temp.element = list.getNode(l).element;
        temp.next = list.getNode(l).next;
        list.getNode(l).element = list.getNode(h).element;
        list.getNode(l).next = list.getNode(h).next;
        list.getNode(h).element = temp.element;
        list.getNode(h).next = temp.next;

I am working on a project for my computer science class and it is due in a couple of days. I want to sort a linked list using quicksort which is fairly simple, but I have been struggling with a strange issue while swapping nodes in the list. For reference, the getNode() function returns a node at a certain index in the list and the nodes hold a pointer to a guitar object along with the a next node pointer. I have been experimenting for hours now and I figured out that the line such as this, "list.getNode(h).element = temp.element;" is saying that the pointer to a guitar object in a node in the list is being assigned the value of the pointer in the temp object. This would normally work but I get an error at this point saying that the left side is not a modifiable lvalue. However I could make a pointer and put it on the left side of the assignment operator without any errors. Also, the left hand side has no error when I use the dereferencing operator (*) in front, but this does not successfully swap the nodes. I must be missing something.

Keith Larson
  • 13
  • 1
  • 4

1 Answers1

2

The return type of your function should be a reference type--otherwise you're telling the compiler to copy construct an object from the value you're returning, and use that as the return value.

I've written an example, which can be found here. It constructs a vector to pass in to the getting function for simplicity's sake, which returns a dereferenced pointer to one of the component objects. If you examine the output of the program, it will merrily inform you that it is copy constructing the value, not getting a reference to it--any changes you make to a node 'retrieved' from the getNode function will be made to a node copy-constructed from the node you want.

I suspect this is also why you're getting an issue with non-modifiable lvalues, since you're attempting to assign values to temporary variables.

tl;dr:
Your getNode function is returning a temporary object. Make it return a reference, instead.

jaggedSpire
  • 4,423
  • 2
  • 26
  • 52
  • I agree with your answer, it was a while ago that I had to learn that same struggle, when I was new to and learning c++! The good thing is once you understand what is happening behind the scenes and know what to look for it eventually gets easier to work with. The only thing I struggle with in c++ anymore is when templates become complex when multiple inheritance & specialization comes into play. I'm not doing to bad for being self taught :) – Francis Cugler May 14 '15 at 03:40