Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: Zodiak98 en 26 Septiembre 2016, 21:37 pm



Título: [AYUDA] ¿Qué tiene de malo este código?
Publicado por: Zodiak98 en 26 Septiembre 2016, 21:37 pm
Verán, estoy intentando hacer un programa bastante simple en C++ pero por alguna razón en la línea 134 me arroja un error y no puedo averiguar por qué.

¿Puede alguien echarme una mano?

Código
  1. #include <iostream>
  2. #include <sstream>
  3. #include <stdio.h>
  4.  
  5. using namespace std;
  6.  
  7. class Cliente
  8. {
  9. public:
  10.  
  11.    Cliente(string nombre, string apellido, long int cedula, short int edad);
  12.  
  13.     // funciones de la clase.
  14.     // tipo de dato a retornar/ nombre / (lista de parametros),.
  15.  
  16.     string getNombre();
  17.     string getApellido();
  18.     long int getCedula();
  19.     short int getEdad();
  20.     string toString();
  21.     void setNombre(string nombre);
  22.     void setApellido(string apellido);
  23.     void setCedula(long int cedula);
  24.     void setEdad(short int edad);
  25.     string convertirCedula(long int cedula);
  26.     string convertirEdad(short int edad);
  27.  
  28. private:
  29.  
  30.    string nombre, apellido;
  31.    long int cedula;
  32.    short int edad;
  33. };
  34.  
  35. // Constructor, fuera de la clase.
  36. Cliente::Cliente(string nombre, string apellido, long int cedula, short int edad)
  37. {
  38.    this->nombre=nombre;
  39.    this->apellido = apellido;
  40.    this->cedula = cedula;
  41.    this->edad = edad;
  42. }
  43.  
  44. string Cliente::getApellido()
  45. {
  46.    return apellido;
  47. }
  48.  
  49. string Cliente::getNombre()
  50. {
  51.  
  52.    return nombre;
  53. }
  54.  
  55. long int Cliente::getCedula()
  56. {
  57.    return cedula;
  58. }
  59.  
  60. short int Cliente::getEdad()
  61. {
  62.    return edad;
  63. }
  64.  
  65. void Cliente::setNombre(string nombre)
  66. {
  67.    this->nombre = nombre;
  68. }
  69.  
  70. void Cliente::setApellido(string apellido)
  71. {
  72.    this->apellido = apellido;
  73. }
  74.  
  75. void Cliente::setCedula(long int cedula)
  76. {
  77.  
  78.    this->cedula = cedula;
  79. }
  80.  
  81. void Cliente::setEdad(short int edad)
  82. {
  83.    this->edad = edad;
  84. }
  85.  
  86. string Cliente::toString()
  87. {
  88.  
  89.    string total;
  90.  
  91.    string cedulaTrans, edadTrans;
  92.  
  93.    cedulaTrans = convertirCedula(cedula);
  94.    edadTrans = convertirEdad(edad);
  95.  
  96.    total = nombre + "\n" + apellido + "\n" + cedulaTrans + "\n" + edadTrans;
  97.  
  98.    return total;
  99. }
  100.  
  101. string Cliente::convertirCedula(long int cedula)
  102. {
  103.      stringstream flujo;
  104.  
  105.      flujo << cedula;
  106.  
  107.      return (flujo.str());
  108. }
  109.  
  110. string Cliente::convertirEdad(short int edad)
  111. {
  112.    stringstream flujo;
  113.  
  114.    flujo << edad;
  115.  
  116.    return (flujo.str());
  117.  
  118. }
  119.  
  120. class Banco
  121. {
  122. public:
  123.  
  124.    Banco();
  125.    string agregar(Cliente c);
  126.    string mostrar();
  127.  
  128. private:
  129.  
  130.    Cliente c[20];
  131.    int cont;
  132. };
  133.  
  134. Banco::Banco()
  135. {
  136.    this->cont=0;
  137.  
  138. }
  139.  
  140. string Banco::agregar(Cliente aux){
  141.  
  142.    if(cont<20)
  143.    {
  144.        c[cont]=aux;
  145.        cont++;
  146.        return "Cliente agregado satisfactoriamente";
  147.    }
  148.  
  149.    return "Banco lleno. No se puede agregar mas personas";
  150.  
  151. }
  152.  
  153. string Banco::mostrar()
  154. {
  155.    string x;
  156.  
  157.    for(int i=0; i<cont; i++)
  158.    {
  159.        x = x + c[i].toString();
  160.    }
  161.  
  162.    return x;
  163.  
  164. }
  165.  
  166. int leerentero(string sms)
  167. {
  168.    int aux;
  169.  
  170.    cout << sms;
  171.    cin >> aux;
  172.  
  173.    return aux;
  174.  
  175. }
  176.  
  177. Cliente leercliente()
  178. {
  179.       string nombre;
  180.       string apellido;
  181.       short int edad;
  182.       long int cedula;
  183.  
  184.       cout << "\nIngrese Nombre: " << endl;
  185.       cin.clear();
  186.       fflush(stdin);
  187.       getline(cin, nombre);
  188.       cout << "\nIngrese Apellido: " << endl;
  189.       cin.clear();
  190.       fflush(stdin);
  191.       getline(cin, apellido);
  192.       cout << "\nIngrese Cedula: " << endl;
  193.       cin >> cedula;
  194.       cout << "\nIngrese Edad: " << endl;
  195.       cin >> edad;
  196.  
  197.       Cliente aux(nombre, apellido, cedula, edad);
  198.       return aux;
  199.  
  200. }
  201.  
  202. int main()
  203. {
  204.    Banco b;
  205.    int opc;
  206.    do
  207.    {
  208.  
  209.        cout << "\n[1]Agregar Persona. \n[2]Mostrar Personas. \n[3]Salir" << endl;
  210.        opc = leerentero("\nIngrese su opcion: ");
  211.  
  212.        switch(opc)
  213.        {
  214.            case 1:
  215.                cout << b.agregar(leercliente()) << endl;
  216.            break;
  217.            case 2: cout << b.mostrar() << endl;
  218.            break;
  219.            case 3: return 0;
  220.            break;
  221.  
  222.        }
  223.  
  224.    }while(opc!=3);
  225.  
  226.    return 0;
  227. }
  228.  
  229.  


Título: Re: [AYUDA] ¿Qué tiene de malo este código?
Publicado por: ivancea96 en 26 Septiembre 2016, 22:07 pm
¿Qué error? Cópialo.


Título: Re: [AYUDA] ¿Qué tiene de malo este código?
Publicado por: crack81 en 26 Septiembre 2016, 22:07 pm
El problema esta en la linea 130 especficamente esta
Código
  1.    Cliente c[20];

Te manda un error porque el arreglo es de tipo cliente y para poder crear un cliente es obligatorio pasarle los parametros:

Código
  1. Cliente(string nombre, string apellido, long int cedula, short int edad);

En cambio tu solo estas declarando el arreglo pero no sabe como construir los objetos ya que la clase cliente solo sabe construir objetos de tipo cliente pansadole los valores del constructor.

La forma mas simple de resolver el problema es sobrecargado el constructor e indicandarle que puede construir objetos de tipo cliente si la necesidad de especificarle los datos al momento de crearlo.


ejemplo sobrecargar constructores:

Código
  1. Cliente();
  2. Cliente(string nombre, string apellido, long int cedula, short int edad);

Código
  1. Cliente::Cliente(){
  2.    //aqui lo inicializas
  3. }
  4. // Constructor, fuera de la clase.
  5. Cliente::Cliente(string nombre, string apellido, long int cedula, short int edad)
  6. {
  7.    this->nombre=nombre;
  8.    this->apellido = apellido;
  9.    this->cedula = cedula;
  10.    this->edad = edad;
  11. }


Hay otras alternativas pero ya dependera de ti.
Saludos... :silbar:


Título: Re: [AYUDA] ¿Qué tiene de malo este código?
Publicado por: Zodiak98 en 26 Septiembre 2016, 22:16 pm
Ya, listo. Funcionó. ¡Gracias, crack81! :)