List linked by reference to a method, c ++

0

I'm in need of help. I'm trying to read a csv file to pass the fields to a linked list and then pass it as a reference to some methods in a class.

while(linea!=VACIO){
    Lista<std::string>*datosCultivo;
    cultivos.datosLista(linea, datosCultivo);
    cargarCultivosJugador(datosCultivo);
    datosCultivo=NULL;
}
void Jugador::cargarCultivosJugador(Lista<std::string>*datos){
    std::string tipoSemilla=datos->obtener(1).c_str();
    std::cout<<tipoSemilla<<std::endl;
    if(tipoSemilla=="A"){
        //this->cultivos->tipoA->agregarInfo(datos);
    }
    else if(tipoSemilla=="B"){
        this->cultivos->tipoB->agregarInfo(datos);
    }
    else if(tipoSemilla=="C"){
        this->cultivos->tipoC->agregarInfo(datos);
    }
    else{
        std::cout<<"No se cargaron los cultivos..."<<std::endl;
    }
}

datosLista I load the fields in the list well, the problem comes when I pass it to cargarCultivosJugador .

It enters the method but when going to addInfo it loses the reference and it goes out of range (It does not get to print anything inside that method and it should obtain values from the list and add them to a pointer object). I clarify that I do not upload all the code because it is very extensive. The truth is how to pass the size of the list by parameter, or should I rethink the problem? I do not have much time for this last, any help is welcome. Greetings

    
asked by Christian 26.05.2018 в 06:21
source

1 answer

2

You have no clear basic C ++ concepts.

You are not passing the list by reference, but by address. You are also passing a memory address that does not point to any list, but it is also a different pointer to each loop loop and to top it all you pass to a function that does not seem to exist.

Your loop.

Let's see what really happens in your loop:

/* 1 */ while(linea!=VACIO){
/* 2 */     Lista<std::string>*datosCultivo;
/* 3 */     cultivos.datosLista(linea, datosCultivo);
/* 4 */     cargarCultivosJugador(datosCultivo);
/* 5 */     datosCultivo=NULL;
/* 6 */ }
  • I do not know what is línea or VACIO , I'll assume it works.
  • You create a pointer to a template object called Lista , this pointer does not point to any existing object .
  • You call member function datosLista of an object called cultivos whose type we do not know, I'll assume it works.
  • You call the function cargarCultivosJugador by passing the pointer that does not point to any object, this function is not the one you show below, since it is not being called associated with any instance of the object Jugador .
  • You make the pointer that did not point to any existing object point to null .
  • Ends the loop, all variables in the scope are destroyed , so datosCultivo ceases to exist.
  • How do you pass by reference?

    Take a look at this thread , then see the changes in your code:

    // Los datosCultivo deben existir FUERA del bucle, para tener persistencia
    Lista<std::string> datosCultivo;
    // Jugador j;
    
    while(linea!=VACIO){
        cultivos.datosLista(linea, datosCultivo);
        // cargarCultivosJugador se llama sobre una instancia de Jugador
        j.cargarCultivosJugador(datosCultivo);
    }
    
    //           Recibe una referencia, no un puntero ---> v
    void Jugador::cargarCultivosJugador(Lista<std::string> &datos){
        //       Punto, no flecha ---> v
        std::string tipoSemilla = datos.obtener(1);
        std::cout << tipoSemilla << '\n';
    
        if(tipoSemilla=="A"){
            //this->cultivos->tipoA->agregarInfo(datos);
        }
        else if(tipoSemilla=="B"){
            this->cultivos->tipoB->agregarInfo(datos);
        }
        else if(tipoSemilla=="C"){
            this->cultivos->tipoC->agregarInfo(datos);
        }
        else{
            std::cout << "No se cargaron los cultivos...\n";
        }
    }
    
        
    answered by 28.05.2018 в 08:03