Apr 24, 2011 at 6:08pm UTC
Every functions defined in the CTime CDate and CDateTime but I still get a C2259 error when i try to make a CDateTime object in main.
namespace MRDateTime
{
class Comparable
{
public:
virtual bool operator==(const Comparable &other)const = 0;
virtual bool operator!=(const Comparable &other)const = 0;
virtual bool operator<(const Comparable &other)const = 0;
virtual bool operator<=(const Comparable &other)const = 0;
virtual bool operator>(const Comparable &other)const = 0;
virtual bool operator>=(const Comparable &other)const = 0;
virtual void input(istream& sin) = 0;
virtual void print(ostream& sout) const = 0;
};
}
namespace MRDateTime
{
static const short HOURS_IN_DAY = 24;
static const short MIN_SEC = 60;
class CTime: virtual public Comparable
{
public:
CTime() {setCurrentTime();};
CTime(short hour, short minute = 0, short second = 0);
short getHour() const {return mHour;};
short getMinute() const {return mMinute;};
short getSecond() const {return mSecond;};
virtual bool operator==(const CTime &time) const;
virtual bool operator!=(const CTime &time) const;
virtual bool operator<(const CTime &time) const;
virtual bool operator<=(const CTime &time) const;
virtual bool operator>(const CTime &time) const;
virtual bool operator>=(const CTime &time) const;
virtual void print(ostream &sout) const;
virtual void input(istream &sin);
void setCurrentTime();
protected:
short mHour;
short mMinute;
short mSecond;
};
ostream& operator<<(ostream& sout, const CTime& time);
istream& operator>>(istream& sin, CTime& time);
}
namespace MRDateTime
{
static const unsigned int FIRSTYEAR = 1760;
static const CDay STARTDAY = TUESDAY;
static const unsigned int TOTALMONTHS = 12;
static const unsigned int TOTALWEEKDAYS = 7;
static const unsigned int TOTALYEARDAYS = 365;
class CDate: virtual public Comparable
{
public:
CDate(short dayOfMth = 1, CMonth month = JANUARY, short year = 1760);
virtual bool operator==(const CDate &date) const;
virtual bool operator!=(const CDate &date) const;
virtual bool operator<(const CDate &date) const;
virtual bool operator<=(const CDate &date) const;
virtual bool operator>(const CDate &date) const;
virtual bool operator>=(const CDate &date) const;
virtual void print(ostream& sout) const;
virtual void input(istream& sin);
static bool isLeapYear(unsigned int year);
static short daysInMth(CMonth &month, short year); //if february and leap year return 29
static string monthNames(short month);
static string dayNames(short day);
CDate yesterday();
CDate tomorrow();
void setCurrentDate();
unsigned int getYear() const {return mYear;};
short getDayOfMth() const {return mDayOfMth;};
CDay getDayOfWk() const {return mDayOfWk;};
CMonth getMonth() const {return mMonth;};
short getDayOfYear() const {return mDayOfYear;};
void setYear(int year) {mYear = year;};
void setDayOfMth(short day) {mDayOfMth = day;};
void setMonth(CMonth &month) {mMonth = month;};
protected:
unsigned int mYear;
short mDayOfMth;
CDay mDayOfWk;
CMonth mMonth;
short mDayOfYear;
private:
static short countLeaps(short year);
void setDayOfYear(short year, CMonth month, short dayOfMonth);
void setDayOfYear(short day) {mDayOfYear = day;};
void setDayOfWeek(short year);
void setDayOfWk(CDay &day) {mDayOfWk = day;};
};
ostream& operator<<(ostream& sout, const CDate& date);
istream& operator>>(istream& sin, CDate& date);
}
namespace MRDateTime
{
class CDateTime: public CDate, public CTime
{
public:
CDateTime() {setCurrentDate(); setCurrentTime();};
CDateTime(const CDate& date, const CTime& time)
:CDate(getDayOfMth(), getMonth(), getYear()), CTime(getHour(), getMinute(), getSecond()) {};
CDateTime(short dayOfMonth, CMonth month = JANUARY, short year= 0, short hour = 0, short minute = 0, short second = 0)
:CDate(dayOfMonth, month, year), CTime(hour, minute, second) {};
virtual void print(ostream& sout) const;
virtual void input(istream& sin);
virtual bool operator==(const CDateTime &dateTime) const;
virtual bool operator!=(const CDateTime &dateTime) const;
virtual bool operator<(const CDateTime &dateTime) const;
virtual bool operator<=(const CDateTime &dateTime) const;
virtual bool operator>(const CDateTime &dateTime) const;
virtual bool operator>=(const CDateTime &dateTime) const;
};
ostream& operator<<(ostream& sout, const CDateTime& dateTime);
istream& operator>>(istream& sin, CDateTime& dateTime);
}
Apr 24, 2011 at 7:01pm UTC
Because the functions you are providing in the CTime, CDate and CDateTime are not
correct virtual overrides.
The original virtual virtual functions in Comparable (such as virtual bool operator==(const Comparable &other)const = 0; ) take a parameter of type Comparable.
The override which you provide in the CTime class for example, look like this virtual bool operator==(const CTime &time) const;
Apr 24, 2011 at 7:36pm UTC
Thanks, I'll try to fix that with a cast or something
Apr 24, 2011 at 8:14pm UTC
Can someone help me with a way to make this work? I can't figure it out
Apr 26, 2011 at 8:32pm UTC
As I said mate- Your virtual function overrides in the various derived classes are not correct!!