I don\'t know why my files aren\'t compiling, Please Help. // file Chrono.h usin
ID: 3830126 • Letter: I
Question
I don't know why my files aren't compiling, Please Help.
// file Chrono.h
using namespace std;
namespace Chrono {
class Date {
public:
enum Month {
jan=1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
};
class Invalid { }; // to throw as exception
Date(int y, Month m, int d); // check for valid date and initialize
Date(); // default constructor
// the default copy operations are fine
// nonmodifying operations:
int day() const { return d; }
Month month() const { return m; }
int year() const { return y; }
// modifying operations:
void add_day(int n);
void add_month(int n);
void add_year(int n);
private:
int y;
Month m;
int d;
};
bool is_date(int y, Date::Month m, int d); // true for valid date
bool leapyear(int y); // true if y is a leap year
bool operator==(const Date& a, const Date& b);
bool operator!=(const Date& a, const Date& b);
ostream& operator<<(ostream& os, const Date& d);
istream& operator>>(istream& is, Date& dd);
Date day_of_week(const Date& d); // day of week of d
Date next_Sunday(const Date& d); // next Sunday after d
Date next_weekday(const Date& d); // next weekday after d
const Date& defualt_date();
} // Chrono
//----------------------------------------------------------------------------------------------
// Chrono.cpp
#include "Chrono.h"
namespace Chrono {
// member function definitions:
Date::Date(int yy, Month mm, int dd)
: y(yy), m(mm), d(dd)
{
if (!is_date(yy,mm,dd)) throw Invalid{};
}
const Date& default_date();
Date::Date()
:y(default_date().year()),
m(default_date().month()),
d(default_date().day())
{
}
int days_in_month(int y, Date::Month m);
void Date:: add_day(int n)
{
for (int i = 0; i < n; i++)
{
d++;
if (!is_date(y, m, d))
{
d = 1;
m = (Month)(m + 1);
if (!is_date(y, m, d))
{
y++;
m = Month::jan;
}
}
}
}
void Date::add_month(int n)
{
// . . .
}
void Date::add_year(int n)
{
if (m==feb && d==29 && !leapyear(y+n)) { // beware of leap years!
m = mar; // use March 1 instead of February 29
d = 1;
}
y+=n;
}
// helper functions:
enum Day {
sunday, monday, tuesday, wednesday, thursday, friday, saturday
};
const Day first_day = monday;
const Date first_date(1601,Date::jan,1);
const Date& default_date()
{
static Date dd {2001,Month::jan,1}; // start of 21st century
return dd;
}
int days_in_month(int y, Date::Month m)
{
switch (m)
{
case Date::feb: // the length of February varies
return (leapyear(y))?29:28;
case Date::apr: case Date::jun: case Date::sep: case Date::nov:
return 30;
default:
return 31;
}
}
bool is_date(int y, Date::Month m, int d)
{
// assume that y is valid
if (d<=0) return false; // d must be positive
if (days_in_month(y,m)<d) return false;
return true;
}
bool leapyear(int y)
{
// see exercise 10
}
bool operator==(const Date& a, const Date& b)
{
return a.year()==b.year()
&& a.month()==b.month()
&& a.day()==b.day();
}
bool operator!=(const Date& a, const Date& b)
{
return !(a==b);
}
ostream& operator<<(ostream& os, const Date& d)
{
return os << '(' << d.year()
<< ',' << static_cast<int>(d.month())
<< ',' << d.day() << ')';
}
istream& operator>>(istream& is, Date& dd)
{
int y, m, d;
char ch1, ch2, ch3, ch4;
is >> ch1 >> y >> ch2 >> m >> ch3 >> d >> ch4;
if (!is) return is;
if (ch1!= '(' || ch2!=',' || ch3!=',' || ch4!=')') { // oops: format error
is.clear(ios_base::failbit); // set the fail bit
return is;
}
dd = Date(y, Month(m),d); // update dd
return is;
}
enum class Day {
sunday, monday, tuesday, wednesday, thursday, friday, saturday
};
Day day_of_week(const Date& d)
{
// . . .
}
Date next_Sunday(const Date& d)
{
// ...
}
Date next_weekday(const Date& d)
{
// . . .
}
Date & Date::operator++()
{
add_day(1);
return *this;
}
} // Chrono
Explanation / Answer
Here is compilable code
#include <iostream>
using namespace std;
namespace Chrono {
class Date {
public:
enum Month {
jan=1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
};
class Invalid { }; // to throw as exception
Date(int y, Month m, int d); // check for valid date and initialize
Date(); // default constructor
// the default copy operations are fine
// nonmodifying operations:
int day() const { return d; }
Month month() const { return m; }
int year() const { return y; }
// modifying operations:
void add_day(int n);
void add_month(int n);
void add_year(int n);
private:
int y;
Month m;
int d;
};
bool is_date(int y, Date::Month m, int d); // true for valid date
bool leapyear(int y); // true if y is a leap year
bool operator==(const Date& a, const Date& b);
bool operator!=(const Date& a, const Date& b);
ostream& operator<<(ostream& os, const Date& d);
istream& operator>>(istream& is, Date& dd);
Date day_of_week(const Date& d); // day of week of d
Date next_Sunday(const Date& d); // next Sunday after d
Date next_weekday(const Date& d); // next weekday after d
const Date& defualt_date();
} // Chrono
Please note for chrono.cpp I have removed operator++ as that is not declared in .h file and have added dummy logic in last three method so that they compile. Hope this helps.
#include "Chrono.h"
namespace Chrono {
// member function definitions:
Date::Date(int yy, Month mm, int dd)
: y(yy), m(mm), d(dd)
{
if (!is_date(yy,mm,dd)) throw Invalid{};
}
const Date& default_date();
Date::Date()
:y(default_date().year()),
m(default_date().month()),
d(default_date().day())
{
}
int days_in_month(int y, Date::Month m);
void Date:: add_day(int n)
{
for (int i = 0; i < n; i++)
{
d++;
if (!is_date(y, m, d))
{
d = 1;
m = (Month)(m + 1);
if (!is_date(y, m, d))
{
y++;
m = jan;
}
}
}
}
void Date::add_month(int n)
{
// . . .
}
void Date::add_year(int n)
{
if (m==feb && d==29 && !leapyear(y+n)) { // beware of leap years!
m = mar; // use March 1 instead of February 29
d = 1;
}
y+=n;
}
// helper functions:
enum Day {
sunday, monday, tuesday, wednesday, thursday, friday, saturday
};
const Day first_day = monday;
const Date first_date(1601,Date::jan,1);
const Date& default_date()
{
static Date dd (2001,Date::jan,1); // start of 21st century
return dd;
}
int days_in_month(int y, Date::Month m)
{
switch (m)
{
case Date::feb: // the length of February varies
return (leapyear(y))?29:28;
case Date::apr: case Date::jun: case Date::sep: case Date::nov:
return 30;
default:
return 31;
}
}
bool is_date(int y, Date::Month m, int d)
{
// assume that y is valid
if (d<=0) return false; // d must be positive
if (days_in_month(y,m)<d) return false;
return true;
}
bool leapyear(int y)
{
// see exercise 10
}
bool operator==(const Date& a, const Date& b)
{
return a.year()==b.year()
&& a.month()==b.month()
&& a.day()==b.day();
}
bool operator!=(const Date& a, const Date& b)
{
return !(a==b);
}
ostream& operator<<(ostream& os, const Date& d)
{
return os << '(' << d.year()
<< ',' << static_cast<int>(d.month())
<< ',' << d.day() << ')';
}
istream& operator>>(istream& is, Date& dd)
{
int y, m, d;
char ch1, ch2, ch3, ch4;
is >> ch1 >> y >> ch2 >> m >> ch3 >> d >> ch4;
if (!is) return is;
if (ch1!= '(' || ch2!=',' || ch3!=',' || ch4!=')') { // oops: format error
is.clear(ios_base::failbit); // set the fail bit
return is;
}
dd = Date(y, Date::Month(m),d); // update dd
return is;
}
Date day_of_week(const Date& d)
{
return Date(0,Date::Month(1),0);
}
Date next_Sunday(const Date& d)
{
return Date(0,Date::Month(1),0);
}
Date next_weekday(const Date& d)
{
return Date(0,Date::Month(1),0);
}
} // Chrono
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.