I have created a Time class with the following overloading of the >>
operator (they use HH:MM:SS format):
inline std::istream& operator>>(std::istream& in, Hora& h) { //Our teacher says we have to implement it with inline and not with friend
std::string aux;
in >> aux;
std::string aux_hora = aux.substr(0, 1);
std::string aux_min = aux.substr(3, 4);
std::string aux_seg = aux.substr(6, 7);
h = Hora(std::stoi(aux_hora), std::stoi(aux_min), std::stoi(aux_seg));
return in;
}
My problem is, how do I have to use the cin
operator then in the main.cpp
? I have tried writing this but the compiler says that I'm using uninitialized variables:
int hora, min, seg;
Hora h(hora, min, seg);
std::cin >> h;
If you need something else, please tell me. Thank you very much.
At first glance, it looks like your problem is in the test code, not the overloaded operator. This code:
int hora, min, seg;
Hora h(hora, min, seg);
...creates an Hora
object, initialized from the current values of hora
, min
, and seg
. But those haven't been initialized...
I'd try something like:
int hora=0, min=0, seg=0;
Hora h(hora, min, seg);
...and see if that fixes the warning. If not, it looks to me like the warning is probably in code you haven't shown us.
Obligatory aside: when/if you want to do something like this in real code (not just an assignment) you probably want to use std::get_time
instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.