I Cant figure out why I keep getting this error, Please help me. My Header: My C
ID: 3832781 • Letter: I
Question
I Cant figure out why I keep getting this error, Please help me.
My Header:
My CPP File
// Chrono.cpp
#include "Chrono.h"
#include "std_lib_facilities_4.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)
{
int old_day = d - 1;
d = 1;
for (int i = 0; i < n; ++i)
{
m = Month(m + 1);
if (!is_date(y, m, d))
{
m = Month::jan;
y++;
}
}
add_day(old_day);
}
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:
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 leapyear(int y)
{
if (y < 0)
{
return false;
}
if (y % 4 == 0)
{
if (y % 100)
return y % 100 == 0;
return true;
}
return false;
}
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;
}
enum Day
{
sunday=0, monday, tuesday, wednesday, thursday, friday, saturday
};
Date day_of_week(const Date& d)
{
return d;
}
Date next_Sunday(const Date& d)
{
return d;
}
Date next_weekday(const Date& d)
{
return d;
}
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;
}
Date & Date::operator++()
{
++d;
if (is_date == false)
{
d = 1;
if (m == dec)
{
m = jan;
++y;
}
else
{
m = Month(int(m) + 1);
}
}
return *this;
}
}
-----------------------------------------------------------------
My Error:
file Chrono.h #include kiostream> using namespace std; namespace Chrono f 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 non modifying operations int day() const return di Month month() const return m; int year const return y; Date & operator 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 true if y is a leap year bool int y) bool operator const Date& a const Date& b) bool operator! (const Date& a const Date& b) ostream& operator os, const Date& d) is tream& 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() ChronoExplanation / Answer
This is not an error first, it is giving a warning but this line needs to modify for working properly.
As per the cpp file, you have a method bool is_date(int y, Date::Month m, int d).
So, to use this method, you are supposed to give 3 parameters while calling that method.
At the line where this warning is given, you have only used the method name, but did not give any argument. So, only the method name is referred as an address where this method gets stored. And an address means a value which is never 0 or NULL. So, it will be always true that means this if() condition does not mean anything.
To remove this error, give the parameters like is_date(y, m, d) such kind. This will remove this warning. See in your code also. At every other place when you called this method, you have given the arguments. But in this line only you did not pass anything. So, give the arguments and the mentioned warning will be removed.
Please comment if there is any query, Thank you. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.