Судя по комментариям, это нерабочий фреймворк для потоковой передачи полумер. НЕ работает - это то, с чем работать.
#include <iostream>
#include <string>
#include <vector>
//-----------------------------------------------------------------------------
struct Date {
int day;
std::string month;
int year;
};
// Date serializing support:
// stream out a Date to a generic stream (like a file)
std::ostream& operator<<(std::ostream& os, const Date& d) {
return os << d.day << '\n' << d.month << '\n' << d.year << '\n';
}
// read a Date from a generic stream (like a file)
std::istream& operator>>(std::istream& is, Date& d) {
return is >> d.day >> d.month >> d.year;
}
// Date adapters for human interaction - only holds references to "real" objects
struct DateUIout {
explicit DateUIout(const Date& d) : date(d) {}
// DateUIout(const Date&) = delete; // not copyable - but made it explicitly
// clear
// holds a reference to the Date struct we'd like to work with
// and an output stream for asking the user questions if needed
const Date& date;
};
std::ostream& operator<<(std::ostream& os, const DateUIout& dui) {
const Date& d = dui.date; // alias to the Date we'd like to work with
return os << "Day: " << d.day << " Month: " << d.month << " Year: " << d.year
<< '\n';
}
struct DateUIin {
explicit DateUIin(Date& d, std::ostream& qs = std::cout) :
date(d), questions(qs) {}
DateUIin(const Date&) = delete; // not copyable - but made it explicitly clear
Date& date;
std::ostream& questions; // used for printing questions to humans
};
std::istream& operator>>(std::istream& is, DateUIin& dui) {
Date& d = dui.date; // alias to the Date we'd like to work with
dui.questions << "Enter the date:\n"
<< " Day: ";
is >> d.day;
dui.questions << " Month: ";
is >> d.month;
dui.questions << " Year: ";
return is >> d.year;
}
//-----------------------------------------------------------------------------
struct Time {
int hr;
int min;
};
// Time serializing support: (much like everything in Date)
// stream out a Time
std::ostream& operator<<(std::ostream& os, const Time& t) {
return os << t.hr << '\n' << t.min << '\n';
}
// read a Time from a stream
std::istream& operator>>(std::istream& is, Time& t) {
return is >> t.hr >> t.min;
}
// Time adapter for human interaction - only holds references to "real" objects
struct TimeUI {
explicit TimeUI(Time& t, std::ostream& qs = std::cout) : tim(t), questions(qs) {}
Time& tim;
std::ostream& questions;
};
std::ostream& operator<<(std::ostream& os, const TimeUI& tui) {
Time& t = tui.tim;
return os << "Hour: " << t.hr << ' ' << t.min << '\n';
}
std::istream& operator>>(std::istream& is, TimeUI& tui) {
Time& t = tui.tim;
tui.questions << "Enter the time:\n Hour: ";
is >> t.hr;
tui.questions << " Minute: ";
return is >> t.min;
}
//-----------------------------------------------------------------------------
struct Timestamp {
Date date{};
Time time{};
};
// Timestamp serializing support:
// stream out a Timestamp
std::ostream& operator<<(std::ostream& os, const Timestamp& ts) {
return os << ts.date << ts.time;
}
// read a Timestamp from a stream
std::istream& operator>>(std::istream& is, Timestamp& ts) {
return is >> ts.date >> ts.time;
}
// Timestamp adapter for human interaction - only holds references to "real" objects
struct TimestampUIout {
TimestampUIout(const Timestamp& Ts) : ts(Ts) {}
// TimestampUIout(const TimestampUI&) = delete;
Timestamp& ts;
};
std::ostream& operator<<(std::ostream& os, const TimestampUIout& tsui) {
return os << DateUIout(tsui.ts.date) << TimeUI(tsui.ts.time, os);
}
struct TimestampUIin {
TimestampUI(Timestamp& Ts, std::ostream& qs = std::cout) :
ts(Ts), questions(qs) {}
TimestampUI(const TimestampUI&) = delete;
Timestamp& ts;
std::ostream& questions;
};
std::istream& operator>>(std::istream& is, TimestampUIin& tsui) {
DateUIin dui(tsui.ts.date, tsui.questions);
TimeUI tui(tsui.ts.time, tsui.questions);
return is >> dui >> tui;
}
//-----------------------------------------------------------------------------
int main() {
Timestamp tmp;
TimestampUI tsui(tmp, std::cout); // bind UI adapter to the Timestamp
std::vector<Timestamp> timestamps;
while(std::cin >> tsui) { // use UI adapter
timestamps.push_back(tmp); // store a raw Timestamp
}
// print collected timestamps - using adapter
for(const Timestamp& ts : timestamps) {
std::cout << TimestampUIout(ts);
}
// print collected timestamps - as they would be streamed to a file
for(const Timestamp& ts : timestamps) {
std::cout << ts;
}
}