0

I'm making an overloaded operator that either adds or subtracts hours. I added a validation so if seconds or minutes are over 60, they change the values accordingly. For this, I'm using setters but they are not working. I read up a bit on this and found out I need to reference the parameters (?) instead of copying them but not sure how to do it. Here is my (partial) code:

    FormatoHora operator+(const FormatoHora &hi, const FormatoHora &hf) {
    int ht, mt, st;
    ht = hi.horas+hf.horas;
    mt = hi.minutos+hf.minutos;
    st = hi.segundos+hf.segundos;

    FormatoHora resultado = *new FormatoHora(ht,mt,st);
    resultado.corregirHora(resultado);
    return resultado;
}

    bool FormatoHora::corregirHora(FormatoHora &i) {
    int v1,v2,v3;
    v1 = getHoras();
    v2 = getMinutos();
    v3 = getSegundos();

        if (v3>60) {
            v3 = v3 - 60;
            v2++;
            //setMinutos(v2);
            //setSegundos(v3);
        }

        if (v2>60) {
            v2 = v2 - 60;
            v1++;
            //setHoras(v1);
            //setMinutos(v2);
        }

        if (v1>24) {
            v1 = v1 - 24;
            //setHoras(v1);
        }
    std::cout << v1;
    std::cout << v2;
    std::cout << v3;

    setHoras(v1);
    setMinutos(v2);
    setSegundos(v3);
    }

I know it is working because when I debug, the values do change inside the variables, but the final output from main shows the original unvalidated values. Main (partial):

    if (opcion == 1) {
            std::cout<<"Hora de inicio: (Hora minutos segundos) \n";
            std::cin >> h1;
            std::cin >> m1;
            std::cin >> s1;

            std::cout<<"Hora de final: (Hora minutos segundos)\n";
            std::cin >> h2;
            std::cin >> m2;
            std::cin >> s2;

            FormatoHora horaS1 = *new FormatoHora(h1,m1,s1);
            FormatoHora horaS2 = *new FormatoHora(h2,m2,s2);

            std::cout<<"La clase dura: \n";
            FormatoHora horaFinalSuma = horaS1 + horaS2;

            horaFinalSuma.to_String();

        }
  • 9
    `FormatoHora resultado = *new FormatoHora(ht,mt,st);` this kind of code is very wrong. You create an object in the heap, copy it to the local object, and leak the object on the heap. Looks like you come from some managed language like Java or C#. I recommend you learn C++ memory management from a book. Correct line would be just `FormatoHora resultado(ht,mt,st);` – Ilya Popov Mar 02 '18 at 17:55
  • 2
    You provided operator -, but you're calling operator + in main. Please provide a [mcve]. – Stephen Newell Mar 02 '18 at 17:56
  • You should take the [tour] and read [Ask] as well. – jwdonahue Mar 02 '18 at 18:11
  • thanks for this, i made the correction. Any books you recommend for a beginner? @StephenNewell sorry, i actually have 2 operators, one for + and one for -. They are basically the same but the + is switched for a -. Will make an edit – pablodavila Mar 02 '18 at 18:24
  • 1
    @KekKekington See here https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Ilya Popov Mar 02 '18 at 18:27

1 Answers1

0

The problem was easy to solve but I didn't notice it. This is the final code CPP (partial):

    FormatoHora operator+(const FormatoHora &hi, const FormatoHora &hf) {
    int ht, mt, st;
    ht = hi.horas+hf.horas;
    mt = hi.minutos+hf.minutos;
    st = hi.segundos+hf.segundos;

    FormatoHora resultado(ht,mt,st);
    resultado.corregirHora(resultado);
    return resultado;
}
bool FormatoHora::corregirHora(FormatoHora &i) {
    int v1,v2,v3;
    v1 = i.getHoras();
    v2 = i.getMinutos();
    v3 = i.getSegundos();

        if (v3>60) {
            v3 = v3 - 60;
            v2++;
            //setMinutos(v2);
            //setSegundos(v3);
        }

        if (v2>60) {
            v2 = v2 - 60;
            v1++;
            //setHoras(v1);
            //setMinutos(v2);
        }

        if (v1>24) {
            v1 = v1 - 24;
            //setHoras(v1);
        }

    i.setHoras(v1);
    i.setMinutos(v2);
    i.setSegundos(v3);
    }

void FormatoHora::setHoras(const int &h) {
    this->horas = h;
}

void FormatoHora::setMinutos(const int &m) {
    this->minutos = m;
}

void FormatoHora::setSegundos(const int &s) {
    this->segundos = s;
}

Main (partial):

if (opcion == 1) {
        std::cout<<"Hora de inicio: (Hora minutos segundos) \n";
        std::cin >> h1;
        std::cin >> m1;
        std::cin >> s1;

        std::cout<<"Hora de final: (Hora minutos segundos)\n";
        std::cin >> h2;
        std::cin >> m2;
        std::cin >> s2;

        FormatoHora horaS1(h1,m1,s1);
        FormatoHora horaS2(h2,m2,s2);

        std::cout<<"La clase dura: \n";
        FormatoHora horaFinalSuma = horaS1 + horaS2;
        std::string nombre =horaFinalSuma.to_String();
        std::cout << nombre;

    }