Search code examples
c++pointersmethodsreferencecall

c++ pointer reference method call


I'm making a program to manage binary trees, but I have a problem here:

void Arbol :: insertar(Nodo*& p, float clave)
{
    if(p == NULL){
        p = new Nodo(clave);
    }else if(clave < p->getclave()){
        insertar(p->getsubarbol_izq(), clave);
    }else{
        insertar(p->getsubarbol_der(), clave);
    }
}

Nodo*& is a reference of the pointer in each node (nodo) in the tree (Arbol).

p->getsubarbol_der() returns a pointer to nodo (nodo*).

But I have this error while calling insertar method of Arbol class: error: no matching function for call to ‘Arbol::insertar(Nodo*, float&)’|


Solution

  • I think the basic problem is that p->getsubarbol_der() returns a temporary, and you're trying to pass that temporary by non-const reference into insertar().