C ++ Unknown error, I have no idea what happens

2

The goal is to format the date correctly, but I need to use that method in the constructor.

#include <cstdlib>
#include <iostream>
#include "fecha.h"

using namespace std;

    class Fecha
{
    public:
        Fecha();
        Fecha(int a, int b, int c);
        void ImprimirFecha();
        void ImprimirMesLetras();
        int Auxiliar (int days, int ultimo);
        int FechaCorrecta (int &day, int &month, int &year);

    protected:
        int dia;
        int mes;
        int anio;
};

Fecha::Fecha()
{
    dia=28;
    mes=8;
    anio=2016;
}

Fecha::Fecha(int a, int b, int c)
{
    Fecha::FechaCorrecta(a, b, c);
    dia=a;
    mes=b;
    anio=c;
}

void Fecha::ImprimirFecha()
{
    cout<<"Fecha Registrada: "<<dia<<"/"<<mes<<"/"<<anio<<endl;
}

void Fecha::ImprimirMesLetras()
{
    switch (mes) {
        case 1: cout<<"Fecha Registrada: "<<dia<<" de Enero de "<<anio<<endl; break;
        case 2: cout<<"Fecha Registrada: "<<dia<<" de Febrero de "<<anio<<endl; break;
        case 3: cout<<"Fecha Registrada: "<<dia<<" de Marzo de "<<anio<<endl; break;
        case 4: cout<<"Fecha Registrada: "<<dia<<" de Abril de "<<anio<<endl; break;
        case 5: cout<<"Fecha Registrada: "<<dia<<" de Mayo de "<<anio<<endl; break;
        case 6: cout<<"Fecha Registrada: "<<dia<<" de Junio de "<<anio<<endl; break;
        case 7: cout<<"Fecha Registrada: "<<dia<<" de Julio de "<<anio<<endl; break;
        case 8: cout<<"Fecha Registrada: "<<dia<<" de Agosto de "<<anio<<endl; break;
        case 9: cout<<"Fecha Registrada: "<<dia<<" de Septiembre de "<<anio<<endl; break;
        case 10: cout<<"Fecha Registrada: "<<dia<<" de Octubre de "<<anio<<endl; break;
        case 11: cout<<"Fecha Registrada: "<<dia<<" de Novienbre de "<<anio<<endl; break;
        case 12: cout<<"Fecha Registrada: "<<dia<<" de Diciembre de "<<anio<<endl; break;
    }
}

int Auxiliar (int days, int ultimo)
{
    if(days<1 && days>ultimo)
        days=0;

    return days;
}

//declaramos la funcion con sus parametros, la cual sera la encargada de realizar el calculo y regresara el resultado
int FechaCorrecta (int &day, int &month, int &year)
{
    bool bisiesto=false;
    int d=day;
//declaramos d,m,Y para contener los resultados de las operaciones, para que no exista alteraciones. y las inicializamos con los valores enviados para devolverlos mismos en caso de un error.
//En este segmento validamos que el año sea bisiesto cumpliendo que sea multiplo de 4, donde la var bisiesto sera de tipo booleano
    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
        bisiesto=true;
    else
        bisiesto=false;
//Aca verificamos cuando el mes sea febrero y el año sea bisiesto, manejando el numero de dias para este caso
//"febrero"=29 dias
    if(month==2 && bisiesto==true)
    {
        d=Auxiliar (day, 2);
    }
//Aca verificamos cuando el mes sea febrero y el año No sea bisiesto, manejando el numero de dias para este caso
//"febrero"=28 dias
    if(month==2 && bisiesto==false)
    {
        d=Auxiliar (day, 28);
    }
//Cuando el mes ingresado sea de 30 dias
//"abril","junio","septiembre","noviembre"=30 dias
    if(month==4 || month==6 || month==9 || month==11)
    {
        d=Auxiliar (day, 30);
    }
//Cuando el mes ingresado sea de 31 dias
//"enero","marzo","mayo","julio","agosto","octubre","diciembre"=31 dias
    if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
    {
        d=Auxiliar (day, 31);
    }

//asignamos los resultados que recogieron las varibles d,m,Y y se las asignamos a las varibles enviadas para que retornen el resultado final.

    if(month<1 && month>12)
    {
        day=0;
        month=0;
    }

    if(year<1)
    {
        day=0;
        month=0;
        year=0;
    }

    day=d;
    return 0;
}

main() {
    //declaramos la clase que contruimos para calcular el area de la superficie y las varibles a utilizar
    int d=0, m=0, y=0;
    int opcion=0;
    char continuar=0, salir=0;
    //imprimimos informacion y luego cactamos los datos para el calculo 
    cout<<" Aplicacion para Registrar la fecha de entrada"<<endl;
    do
    {
        cout<<"Ingresar fecha de entrada. Introduzca el dia/mes/a#o: "<<endl;
        cin>>d>>m>>y;
        Fecha day(d,m,y);
        system("cls");
        cout<<"----------------------Menus------------------"<<endl;
        cout<<"1.-Imprimir fecha registrada dia/mes/a#0."<<endl;
        cout<<"2.-Imprimir fecha con el mes en palabras."<<endl;
        cout<<"3.-Modificar fecha registrada."<<endl;
        cout<<"4.-Salir."<<endl;
        cout<<"----------------------------------------------------------"<<endl;
        //imprimimos
        do
        {
            cout<<"Elegir una opcion para continuar: ";
            cin>>opcion;
            salir='n';

            switch (opcion)
            {
                case 1:
                    day.ImprimirFecha();
                    continuar='s';
                break;
                case 2:
                    day.ImprimirMesLetras();
                    continuar='s';
                break;
                case 3:
                    continuar='n';
                    salir='s';
                break;
                case 4:
                    cout<<"Si continuar salir presione la tecla 'S' sino 'N': ";
                    cin>>continuar;
                    salir='n';
                break;
            }

        }
        while(continuar=='s');

    }
    while(salir=='s');

    system("PAUSE");
    return EXIT_SUCCESS;
}

    
asked by Mario P 29.08.2016 в 08:17
source

1 answer

3
class Fecha
{
    public:
        int FechaCorrecta (int &day, int &month, int &year);
};

Fecha::Fecha(int a, int b, int c)
{
    Fecha::FechaCorrecta(a,b,c);
}

The type calls Clase::Funcion() are used for static functions and to call functions of the parent class hidden by the implementation of the child class (which is not your case). The problem in your case is that the function FechaCorrecta is not declared as static. For readability you should apply one of these two options:

  • Declare the function as static
  • Remove the prefix of the class in the call to the function:
  • Said with code:

    // Opción 1
    class Fecha
    {
        public:
            static int FechaCorrecta (int &day, int &month, int &year);
    };
    
    // Opción 2
    Fecha::Fecha(int a, int b, int c)
    {
        FechaCorrecta(a,b,c);
    }
    

    Regarding your error: Look at the implementation of FechaCorrecta and implementation of ImprimirFecha :

    void Fecha::ImprimirFecha()
    {
        cout<<"Fecha Registrada: "<<dia<<"/"<<mes<<"/"<<anio<<endl;
    }
    
    int FechaCorrecta (int &day, int &month, int &year)
    {
    }
    

    The difference is obvious. In FechaCorrecta you need to include the prefix of the class to indicate that this implementation corresponds to the function defined within the class Fecha :

    int Fecha::FechaCorrecta (int &day, int &month, int &year)
    {
    }
    

    And the same thing happens with the Auxiliar function.

    Greetings.

        
    answered by 29.08.2016 в 08:28