elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Trabajando con las ramas de git (tercera parte)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  Problema Con Un Arreglo Dinamico De Estructuras [?]
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Problema Con Un Arreglo Dinamico De Estructuras [?]  (Leído 2,489 veces)
Omar_2013

Desconectado Desconectado

Mensajes: 26


Ver Perfil
Problema Con Un Arreglo Dinamico De Estructuras [?]
« 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


« Última modificación: 2 Septiembre 2013, 19:00 pm por Omar_2013 » En línea

amchacon


Desconectado Desconectado

Mensajes: 1.211



Ver Perfil
Re: Problema Con Un Arreglo Dinamico De Estructuras [?]
« Respuesta #1 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. }


En línea

Por favor, no me manden MP con dudas. Usen el foro, gracias.

¡Visita mi programa estrella!

Rar File Missing: Esteganografía en un Rar
Omar_2013

Desconectado Desconectado

Mensajes: 26


Ver Perfil
Re: Problema Con Un Arreglo Dinamico De Estructuras [?]
« Respuesta #2 en: 2 Septiembre 2013, 19:21 pm »

Gracias.
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Arreglo Dinamico con Objetos :S
Java
Xephiro 9 14,150 Último mensaje 6 Marzo 2009, 20:20 pm
por juancho77
Ayuda. Problema con arreglo de estructuras.
Programación C/C++
Gorka82 1 3,012 Último mensaje 11 Julio 2010, 06:26 am
por Littlehorse
Problema con estructuras
Programación C/C++
clodan 3 2,901 Último mensaje 14 Septiembre 2010, 18:41 pm
por Garfield07
arreglo de estructuras
Programación C/C++
mapers 0 2,277 Último mensaje 11 Marzo 2011, 07:56 am
por mapers
Duda con arreglo bidimensional dinámico en IDE DevC++
Programación C/C++
finger10 3 7,744 Último mensaje 2 Septiembre 2011, 17:01 pm
por finger10
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines