Invalid conversion from 'int' to 'Class **'

1

I'm doing a program that orders a list of 3 different ways (by selection, bubble and shell method). I get the following error

Invalid conversion from 'int' to 'Animal**'

and

void value not ignored as it ought to be

in these lines

                            seleccion=ordSeleccion(a,n);

                            burbuja=ordBurbuja(a,n);

                            shell=ordShell(a,n);

of this code

                        int opcbus,a,n,seleccion,burbuja,shell;

                      do{
                      cout<<"Elija el método de ordenamiento a utilizar \n 1) Metodo seleccion \n 2) Metodo Burbuja \n 3) Metodo Shell \n 4) Regresar \n"<<endl;
                      cin>>opcbus;
                      switch (opcbus)
                      {
                        case 1:
                            seleccion=ordSeleccion(a,n);
                            break;
                        case 2:
                            burbuja=ordBurbuja(a,n);
                            break;
                        case 3:
                            shell=ordShell(a,n);
                            break;
                        case 4:
                            break;          
                      case '5':             
                      break; 

these are my orders

        void ordSeleccion (Animal *a[], int n){
            int indiceMenor,i,j;
            Animal *aux;
            for (i=0;i<n-1;i++){        
                indiceMenor=i;
                for (j=i+1;j<n-1;j++)
                if(a[j]->getNombreAnimal()>a[indiceMenor]->getNombreAnimal())
                indiceMenor=j;
                if (i!=indiceMenor){
                    aux=a[i];
                    aux=a[indiceMenor];
                    a[indiceMenor]=aux;
                }
            }
        };

        void ordBurbuja (Animal *a[], int n){
            int interruptor=1,pasada,j;
            Animal *aux;
            for (pasada=0;pasada<n-1&&interruptor;pasada++){
                interruptor=0;
                for (j=0;j<n-pasada-1;j++)
                if(a[j]->getNombreAnimal()>a[j+1]->getNombreAnimal()){                      
                interruptor=1;  
                aux=a[j];
                a[j]=a[j+1];
                a[j+1];
                }
            }
            cout<<"Los animales en orden son:\n"<<endl;
        };

        void ordShell (Animal *a[], int n){
            int intervalo,i,j,k;
            Animal *aux;
            intervalo=n/2;
            while(intervalo>0){
                for (i=intervalo;i<n;i++){
                    j=i-intervalo;
                    while (j>=0){
                        k=j+intervalo;
                        if (a[j]->getNombreAnimal()<=a[k]->getNombreAnimal())
                        j=1;
                        else{
                            aux=a[j];
                            a[j]=a[k];
                            a[k]=aux;
                            j-=intervalo;
                        }
                    }
                    intervalo=intervalo/2;                  
                }
            }
        };
    
asked by casebubble 14.07.2017 в 16:55
source

1 answer

1

Problem.

The errors are crystal clear and indicate your problem with total precision; maybe you miss the meaning of being in English, let me translate them:

Invalid conversion from 'int' to 'Animal**'
Conversión no válida de 'int' a 'Animal**'

The compiler is telling you that it can not convert an integer ( int ) in a pointer to a pointer to Animal . Which is obvious, a int is not a Animal .

This will happen to you when you call the functions ordSeleccion , ordBurbuja and ordShell whose signatures are:

  • void ordSeleccion (Animal *a[], int n)
  • void ordBurbuja (Animal *a[], int n)
  • void ordShell (Animal *a[], int n)

And what have you called as follows:

  • seleccion=ordSeleccion(a,n);
  • burbuja=ordBurbuja(a,n);
  • shell=ordShell(a,n);

With a and n both variables of type int , you have declared them as such in:

int opcbus,a,n,seleccion,burbuja,shell;

So, while n matches the type of the second parameter of your functions, the variable a does not match this int and the first parameter being Animal *a[] (pointer to% fix Animal ).

Regarding the second error:

void value not ignored as it ought to be
El valor void no es ignorado y debería serlo

In C ++ void means " empty " 1 , when you want a function not to return values you indicate that the return of the function is empty, making it return void , that way the compiler knows that it should not wait for any value of the function and can ignore its return ... however you say one thing to the compiler and you act opposite to what you say, remember the signature of the functions ordSeleccion , ordBurbuja and ordShell :

  • void ordSeleccion (Animal *a[], int n)
  • void ordBurbuja (Animal *a[], int n)
  • void ordShell (Animal *a[], int n)

In all of them you tell the compiler: "Do not worry, I do not plan to return anything with these functions", but immediately afterwards in the code:

  • seleccion=ordSeleccion(a,n);
  • burbuja=ordBurbuja(a,n);
  • shell=ordShell(a,n);

You tell the compiler: "Whatever the function returns, you keep it in these variables of type int ", the compiler is confused and refuses to compile: " What are we left with? Should I or should I not ignore the return of those functions!? "

Solution.

Passes a pointer to Animal as first parameter to functions ordSeleccion , ordBurbuja and ordShell and do not put them to the right of an assignment.

1 That's what it means in English, which coincidentally is the same as it means in C ++.

    
answered by 17.07.2017 в 09:11