Can not convert an int to struct in c ++

0

I have a problem with a function that the professor gave us, he did it but at the time of compiling it in Borland he shows me the error "Can not convert 'int' to 'NodoAvl', being NodoAvl the name of the struct. Function code.

Function:

NodoAvl* InsertarAvl(NodoAvl *raiz, int dato, bool &hc){
    NodoAvl *n1=NULL;<br>
    if (raiz == NULL){
            raiz = new NodoAvl (dato);  **Error cannont convert 'int' to 'strcut'**
            hc = true;
    }else{
            if (dato < raiz->Info){
                    NodoAvl *iz;
                    iz = InsertarAvl(raiz->Izq, dato, hc);
                    raiz->Izq=iz;
                    // regreso por los nodos del camino de búsqueda
                    if (hc){ // siempre se comprueba si creció en altura                      // decrementa el FE por aumentar la altura de rama izquierda
                            switch (raiz->FE){
                                  case 1: // tenía +1 y creció su izquierda
                                        raiz->FE=0;
                                        hc = false; // árbol deja de crecer
                                        break;
                                  case 0: // tenía 0 y creció su izquierda
                                        raiz->FE=-1; //árbol sigue creciendo
                                        break;
                                  case -1: //aplicar rotación a la izquierda
                                        n1 = raiz->Izq;
                                        if (n1->FE == -1)
                                        raiz = RotacionII(raiz, n1);
                                           else
                                           raiz = RotacionID(raiz, n1);
                                           hc = false; 
                            }
                    }
            }else{ 
                    if (dato > raiz->Info){
                        NodoAvl *dr;
                        dr = InsertarAvl(raiz->Der, dato, hc);
                        raiz->Der = dr;
                        // regreso por los nodos del camino de búsqueda
                        if (hc){ // siempre se comprueba si creció en altura
                        //incrementa el FE por aumentar la altura de rama izquierda
                        switch (raiz->FE){
                          case 1: // aplicar rotación a la derecha
                              n1 = raiz->Der;
                              if (n1->FE == 1)
                              raiz = RotacionDD(raiz, n1);
                              else
                              raiz = RotacionDI(raiz,n1);
                              hc = false; 
                              break;
                          case 0: // tenía 0 y creció su derecha
                              raiz->FE = 1; // árbol sigue creciendo
                              break;
                          case -1: // tenía -1 y creció su derecha
                              raiz->FE = 0;
                              hc = false; // árbol deja de crecer
                                    }
                            }
                    }else
                            throw "No puede haber claves repetidas " ;
            }        
    }
    return raiz;
}
    
asked by GersonRod_ 03.04.2016 в 00:14
source

2 answers

2

As noted by rnd and eferion , it is a problem of absence of constructor; the error shown in a similar case by CLang is slightly more enlightening:

error: no matching constructor for initialization of 'NodoAvl'
auto na = new NodoAvl(666);
              ^       ~~~
note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'int' to 'const NodoAvl' for 1st argument
struct NodoAvl
       ^
note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'int' to 'NodoAvl' for 1st argument
struct NodoAvl
       ^
note: candidate constructor (the implicit default constructor) not viable: requires 0 arguments, but 1 was provided

You can see it here .

However, if your NodoAvl is (or was) an aggregate, you could use aggregate initialization to solve the problem:

raiz = new NodoAvl {dato};

Note that I use braces instead of parentheses; in this way I do not call the constructor with int (which it lacks) but the constructor as an aggregate.

On the other hand, you have an additional problem with your code, I do not know how you use the InsertarAvl function but on the line where you assign value to the% po_de%, which you receive as an input parameter, you are not modifying the pointer received if not a copy, so, using the function like this:

NodoAvl *n = NULL;
bool hc;
InsertarAvl(n, 0xFabada, hc);

The pointer raiz would remain n after the call to NULL ; If you want to modify the pointer, you must pass it as a pointer to a pointer or as a reference to a pointer:

NodoAvl* InsertarAvl(NodoAvl **raiz, int dato, bool &hc){
    NodoAvl *n1=NULL;
    if (raiz == NULL){
            *raiz = new NodoAvl (dato);
            hc = true;
    }
    ...
    ...

NodoAvl* InsertarAvl(NodoAvl *&raiz, int dato, bool &hc){
    NodoAvl *n1=NULL;
    if (raiz == NULL){
            raiz = new NodoAvl (dato);
            hc = true;
    }
    ...
    ...

I prefer reference to pointer.

    
answered by 04.04.2016 в 11:16
1
raiz = new NodoAvl (dato);

In that line the program is trying to call the constructor

NodoAvl(int);

If such a constructor does not exist then the compiler tries an implicit conversion. If it turns out that the compiler is unable to find the implicit conversion, the compilation will end in error.

In your case, everything points to the fact that you have to implement the corresponding constructor in NodoAvl .

If this is not the problem you will have to put more information, as indicated by @rnd.

Greetings.

    
answered by 04.04.2016 в 10:30