Witam, jednym z moich zadań było stworzenie struktury opisującej czas, która będzie zawierała funkcje składowe takiej jak, zwiekszanie godzin, minut i sekund o 1, czy też wypisanie pory dnia zależnej od godziny. Teraz mam do tej struktury dodać dane funkcje umożliwiające arytmetykę na czasie:
auto operator + ( Time const &) const -> Time ;
auto operator - ( Time const &) const -> Time ;
auto operator < ( Time const &) const -> bool ;
auto operator > ( Time const &) const -> bool ;
auto operator == ( Time const &) const -> bool ;
auto operator != ( Time const &) const -> bool ;
Mają one być zaimplementowane według podanego przykładu:
//plik naglowkowy
struct Modulo_ arithmetic {
int const mod { std :: numerical_limits < int >:: max () };
int value { 0 };
Modulo_arithmetic ( int v , int m ): mod { m } , value { v } {}
auto operator + ( Modulo _ arithmetic const ) const -> Modulo _ arithmetic ;
auto operator < ( Modulo _ arithmetic const ) const -> bool ;
};
//plik glowny
auto Modulo _arithmetic :: operator + ( Modulo _ arithmetic const o ) const
-> Modulo_ arithmetic
{
// FIXME what if the mods are different ?
return Modulo_ arithmetic {( value + o . value ) % mod , mod };
}
auto Modulo _arithmetic :: operator < ( Modulo _ arithmetic const o ) const
-> bool
{
return ( value < o . value );
}
Oto mój kod (plik nagłówkowy):
#include <iostream>
#include <string>
struct Time {
int godzina;
int minuta;
int sekunda;
/* funkcje potrzebne do poprzednich zadań
auto to_string() const -> std::string;
auto next_hour() -> void;
auto next_minute() -> void;
auto next_second() -> void;
enum class Time_of_day { Niezdefiniowany, Rano, Dzien, Wieczor, Noc };
auto time_of_day() const -> Time_of_day;
auto to_string(Time_of_day) -> std::string;
*/
auto operator+ ( Time const ) const -> Time ;
auto operator- ( Time const ) const -> Time ;
auto operator< ( Time const ) const -> bool ;
auto operator> ( Time const ) const -> bool ;
auto operator== ( Time const ) const -> bool ;
auto operator!= ( Time const ) const -> bool ;
Time(int godzina, int minuta, int sekunda);
};
Plik główny:
#include <s25441/Time.h>
#include <iomanip>
#include <iostream>
#include <string>
Time::Time(int godz, int min, int sek)
: godzina{godz}, minuta{min}, sekunda{sek}
{}
/* funkcje potrzebne do poprzednich zadań
auto ::Time::next_hour() -> void
{
if (godzina < 23) {
godzina++;
} else {
godzina = 0;
}
}
auto ::Time::next_minute() -> void
{
if (minuta < 59) {
minuta++;
} else {
next_hour();
minuta = 0;
}
}
auto ::Time::next_second() -> void
{
if (sekunda < 59) {
sekunda++;
} else {
next_minute();
sekunda = 0;
}
}
auto ::Time::to_string() const -> std::string
{
std::string godz = std::to_string(godzina);
std::string min = std::to_string(minuta);
std::string sek = std::to_string(sekunda);
if (godzina < 10) {
godz = "0" + godz;
}
if (minuta < 10) {
min = "0" + min;
}
if (sekunda < 10) {
sek = "0" + sek;
}
return godz + ":" + min + ":" + sek;
}
auto ::Time::time_of_day() const -> Time_of_day
{
if ((godzina == 23) || ((godzina >= 0) && (godzina <= 5))) {
return Time_of_day::Noc;
} else if ((godzina >= 6) && (godzina <= 10)) {
return Time_of_day::Rano;
} else if ((godzina >= 11) && (godzina <= 16)) {
return Time_of_day::Dzien;
} else if ((godzina >= 17) && (godzina <= 22)) {
return Time_of_day::Wieczor;
} else {
return Time_of_day::Niezdefiniowany;
}
}
auto ::Time::to_string(Time_of_day t) -> std::string
{
if (t == Time_of_day::Rano) {
return "Rano";
} else if (t == Time_of_day::Dzien) {
return "Dzien";
} else if (t == Time_of_day::Wieczor) {
return "Wieczor";
} else if (t == Time_of_day::Noc) {
return "Noc";
} else {
return "Niezdefiniowany";
}
}
*/
auto :: Time :: operator+ (Time const o) const -> Time
{
return Time (godzina + o.godzina);
}
auto main() -> int
{
auto czas = Time(23, 59, 59);
auto czas2 = Time(10, 46, 1);
std::cout << czas.to_string() << std::endl;
std::cout << czas.to_string(czas.time_of_day()) << std::endl;
czas.next_minute();
std::cout << czas.to_string() << std::endl;
czas.next_second();
std::cout << czas.to_string() << std::endl;
czas.next_hour();
std::cout << czas.to_string() << std::endl;
std::cout << czas2.to_string() << std::endl;
std::cout << czas2.to_string(czas2.time_of_day()) << std::endl;
return 0;
}
Mam w sumie 3 problemy:
1. Po skompilowaniu programu, wyświetla się błąd: "error: no matching function for call to ‘Time::Time(int)’
return Time (godzina + o.godzina);"
2. Jak zdefniować te funkcje, aby możliwe były operacje na całym czasie, a nie tylko na godzinie, jak początkowo chciałem to zrobić?
3. Jak wywołać taką funkcję w mainie?
Mam nadzieję, że to co napisałem jest w miarę przejrzyste. Z góry dziękuję za pomoc!