Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: Omar_2013 en 2 Septiembre 2013, 18:56 pm



Título: Problema Con Un Arreglo Dinamico De Estructuras [?]
Publicado por: Omar_2013 en 2 Septiembre 2013, 18:56 pm
#include <cstdlib>
#include <iostream>


using namespace std;

struct Datos {
       char *Nombre;
       int Edad;
};

struct Categorias {
       char *Deporte;
       int Atletas;
       Datos *Informacion;
};

int main(int argc, char *argv[])
{  
    Categorias *Otro= new Categorias[10];
    
    Otro[0].Informacion->Nombre="Oscar";
        
    system("PAUSE");
    return EXIT_SUCCESS;
}


Pordrian por favor ayudarme .. el codigo compila pero el ejecutable se totea


Título: Re: Problema Con Un Arreglo Dinamico De Estructuras [?]
Publicado por: amchacon en 2 Septiembre 2013, 19:14 pm
En la estructura, Información es un puntero y no un objeto. Si no inicializas el puntero te va a dar error.

Solución 1, convertirlo de puntero a objeto:

Código
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. struct Datos {
  7.       char *Nombre;
  8.       int Edad;
  9. };
  10.  
  11. struct Categorias {
  12.       char *Deporte;
  13.       int Atletas;
  14.       Datos Informacion;
  15. };
  16.  
  17. int main(int argc, char *argv[])
  18. {
  19.    Categorias *Otro= new Categorias[10];
  20.  
  21.    Otro[0].Informacion.Nombre = "Miau";
  22.  
  23.    return EXIT_SUCCESS;
  24. }
  25.  

Solución 2:

Código
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. struct Datos {
  7.       char *Nombre;
  8.       int Edad;
  9. };
  10.  
  11. struct Categorias {
  12.       char *Deporte;
  13.       int Atletas;
  14.       Datos *Informacion;
  15. };
  16.  
  17. int main(int argc, char *argv[])
  18. {
  19.    Categorias *Otro= new Categorias[10];
  20.  
  21.    Otro[0].Informacion = new Datos;
  22.  
  23.    Otro[0].Informacion->Nombre= "Oscar";
  24.  
  25.    return EXIT_SUCCESS;
  26. }

Solución 3, usar un constructor que lo inicialize:

Código
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. struct Datos {
  7.       char *Nombre;
  8.       int Edad;
  9. };
  10.  
  11. struct Categorias {
  12.       char *Deporte;
  13.       int Atletas;
  14.       Datos *Informacion;
  15.  
  16.       Categorias() { Informacion = new Datos;}
  17. };
  18.  
  19. int main(int argc, char *argv[])
  20. {
  21.    Categorias *Otro= new Categorias[10];
  22.  
  23.    Otro[0].Informacion->Nombre= "Oscar";
  24.  
  25.    return EXIT_SUCCESS;
  26. }
  27.  

Solución 4, inicializar nombre desde un constructor:

Código
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. struct Datos {
  7.       char *Nombre;
  8.       int Edad;
  9.  
  10.       Datos(char* nombre) : Nombre(nombre) {}
  11. };
  12.  
  13. struct Categorias {
  14.       char *Deporte;
  15.       int Atletas;
  16.       Datos *Informacion;
  17.  
  18.       Categorias(char* Nombre = NULL) { Informacion = new Datos(Nombre);}
  19. };
  20.  
  21. int main(int argc, char *argv[])
  22. {
  23.    Categorias *Otro= new Categorias("Oscar");
  24.  
  25.    return EXIT_SUCCESS;
  26. }


Título: Re: Problema Con Un Arreglo Dinamico De Estructuras [?]
Publicado por: Omar_2013 en 2 Septiembre 2013, 19:21 pm
Gracias.