node &getNode(int index) {                                                 //NOT TESTED
        if (index <= getLength() || index >= 1){
            temp = new node;
            temp = head;
            for (int i=index-1; i>0; i--){
                temp = temp->next;
            }
            return &temp;
        }   
    }

Hi everyone,
I receive this error msg:

invalid initialization of non-const reference of type 'node&' from a temporary of type 'node**'

I cannot change the return type of the method so how can i return that reference.
Thanks

Edited 3 Years Ago by pyTony: fixed formatting

node &getNode(int index) { //NOT TESTED
if (index <= getLength() || index >= 1){
temp = new node;
temp = head;
for (int i=index-1; i>0; i--){
temp = temp->next;
}
return &temp;
}
}

Hi everyone,
I receive this error msg: "invalid initialization of non-const reference of type 'node&' from a temporary of type 'node**' "

I cannot change the return type of the method so how can i return that reference.
Thanks

I really dont know whats going on here, there are a few things that are weird.

One, you method should be returning Node* not Node&. Also, when you declare a new object of type Node, it should be

Node* temp = new Node;

If you do those things then you don't have to return anything special, just return temp.

This article has been dead for over six months. Start a new discussion instead.