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();
}