How to change where a pointer points to? C++

Hello again,

I have yet another issue that I can't seem to get around.
I have a class called node, which has two private fields: a data field, and a link field. These nodes form a linked list, with one node's link field pointing to the next node, and so on, until the final node's link which is NULL.

I am trying to change the order of the nodes...and to do so I have to change the link field of the node. I am having an issue changing the links in the node. I thought I had an understanding of exactly how to do it. Consider this:

head_ptr points to the first node. The link of the first node points to the second node. I want to change head_ptr to "skip" over the second node and make it point to the third node. A pointer called marker_ptr points to the third node. Essentially, I want to make the link field (which is a pointer) that head_ptr points to, point to the node that marker_ptr points to. I have backup pointers to each of these first three nodes so that they do not become "unlinked" from the list. Here is my line of code that I thought should perform this task:
Note: the node class definition is in a header file. This code is in my implementation file.
 
head_ptr->node::link() = marker_ptr;

Correct me if I'm wrong, but the above code means: Change the link of the node that head_ptr points to to what marker_ptr points to.

Upon compiling I get this error:
error: lvalue required as left operand of assignment

This error points to that line of code I have written above.

I feel like I'm missing an operator, but I'm kind of confused as to what the de-referencing and address operators will actually do in this case. Thanks for your help, this forum has been super helpful thus far.
Hold that thought...I just got an idea.

Instead of using a pointer to the third node, I need a pointer to the second node's link field, and use the second node's link field (which points to the third node, just like marker_ptr does).

I will try that and get back to this thread, but I wanted to let you know beforehand in case this is the solution...so you don't waste your time.

EDIT: No luck, it's been about 2 hours of me messing around trying different things to get these pointers redirected. I'm still having trouble with actually changing the private "link" field in the nodes. I cannot delete/add nodes. I have to change the link field of the node to point to another node.
Last edited on
The error message clearly describes your problem. You trying to assign something to an rvalue. Hint: Check your return type for that link() method.

Did you take some time to try to understand error message? You should be able to figure out the solution right away after doing so.
Last edited on
I'm still not understanding what the error message means. What is an rvalue and an lvalue? I would assume left and right of the equals sign?

There are two link() functions in my header file, inside the node class definition. They are written in line since they're so short...and the declaration of link_field follows below:
1
2
3
4
5
const node* link( ) const { return link_field; }
node* link( )             { return link_field; }

private:
node* link_field;


I suppose that now after looking at the return type of the link() function (which is a pointer to a node), I need to modify the function in my header file to make it pass the link_field by reference?

EDIT: After looking at my idea above in terms of my code, my idea doesn't make sense. link() is not a function to change the link, it only returns the link_field. I see what my error is in this bit head_ptr->node::link() = marker_ptr; but I still don't understand what the compiler's error message is saying.

In my header file there is a pre-written function called set_link(node* new_link) which will change link_field. I wasn't sure how to apply this function into my code yesterday, but I guess I will HAVE to find a way to do it now. Thanks for pointing me in the direction to look vince1027...now I realize what should've been obvious yesterday. The only way to change the PRIVATE link_field variable is with a member function in the header file. I was trying to assign a new value to link_field from outside the class.

Can anyone explain what my compiler's error message above means, anyways? Thanks.

Last edited on
closed account (o1vk4iN6)
You would need a double pointer to change a pointer :O, the horrors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

class Node
{
public:
     Node** link(){ return &next; }
private:
     Node* next;

};

int main()
{
     Node a, b;

     *a.link() = &b;
}


Or you can just go the easier route of a nicer design.

1
2
3
4
5
6
7
8
9
10
class Node
{
public:
     SetLink( Node* node ){ next = node; }

private:
    Node* next;
};

Last edited on
Great. Your second example is how my header file is written...so I'm glad it's not as confusing as is could be! I got the links to change using my set_ptr function, so this thread is solved.

Thanks for your help! :)
What is an rvalue and an lvalue? I would assume left and right of the equals sign?

In a sense, yes. That's the easiest way to wrap your head around it. The compiler is basically complaining because you are attempting to assign something to a temporary value (referred as 'rvalue'). You can view it visually this way:
1
2
3
4
int functionInt(void);

int my = functionInt(); // functionInt() returns a temporary here that will be assigned (technically copied) to the "my" variable.
functionInt() = 22; // this line attempts to assign something to the temporary returned by functionInt(). This is where compiler will complain. 


You would need a double pointer to change a pointer :O, the horrors.

Not necessarily. This is where C++ references comes in handy. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Node
{
public:
    Node*& link(void)
    {
        return (next);
    }
private:
    Node* next;
};

int main(void)
{
    Node a;
    a.link() = nullptr;
    return (0);
}
Thanks Vince!
Topic archived. No new replies allowed.