user3136756
user3136756

Reputation: 11

lvalue required as unary '&' operand error in C++

I am trying to pass by reference a value to another pointer, however I am getting the error: lvalue required as unary '&' operand.

The following are my attempts:

Node<Item,Key> *root;

Node<Item, Key> *x= root;

x= &x->getLeft(); // it does not let me use &

The following is my getter for getLeft():

template<typename Item, typename Key>
Node<Item, Key>* Node<Item, Key>::getLeft() {

    return left;
}

I appreciate if someone could identify my mistake. Thanks in advance.

Upvotes: 0

Views: 3662

Answers (2)

Joseph Mansfield
Joseph Mansfield

Reputation: 110728

x->getLeft() returns a Node<Item, Key>* - it's already a pointer. If you take the address of it, you'd get a Node<Item, Key>**, which is not something you can assign to x.

You appear to another problem though. Your root pointer is uninitialized. Then you're copying it over to x and trying to access a member by indirection (->). You're going to need to make root and x point at something before you do that.

Upvotes: 0

Kerrek SB
Kerrek SB

Reputation: 477408

You mean:

x = x->getLeft();

The return value of getLeft() is already a pointer.

Upvotes: 4

Related Questions