X
xz
I am coding for this little class Date, which represents the date
consisting of year, month and day.
The header file is as follows:
#ifndef DATE_H
#define DATE_H
class Date {
static const int daysInMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31,
30, 31, 30, 31};
public:
int year;
int month;
int day;
bool isLeap;
public:
Date(int y, int m, int d):year(y), month(m), day(d) {
isLeap = isLeapYear();
}
bool isLeapYear();
static bool isLeapYear(int year);
int dayInTheYear();
friend int operator-(const Date& left, const Date& right);
friend int operator==(const Date& left, const Date& right);
friend int operator!=(const Date& left, const Date& right);
friend int operator>(const Date& left, const Date& right);
friend int operator<(const Date& left, const Date& right);
};
#endif //DATE_H
However, the 5th line (static const int daysInMonth[] = {0, 31, 28,
31, 30, 31, 30, 31, 31, 30, 31, 30, 31} does not compile.
The error information is :
Date.h:5: error: a brace-enclosed initializer is not allowed here
before '{' token
Date.h:5: error: invalid in-class initialization of static data member
of non-integral type 'const int []'
This line is to save and provide the numbers of the days in the
months.
How could I implement what I want ?
consisting of year, month and day.
The header file is as follows:
#ifndef DATE_H
#define DATE_H
class Date {
static const int daysInMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31,
30, 31, 30, 31};
public:
int year;
int month;
int day;
bool isLeap;
public:
Date(int y, int m, int d):year(y), month(m), day(d) {
isLeap = isLeapYear();
}
bool isLeapYear();
static bool isLeapYear(int year);
int dayInTheYear();
friend int operator-(const Date& left, const Date& right);
friend int operator==(const Date& left, const Date& right);
friend int operator!=(const Date& left, const Date& right);
friend int operator>(const Date& left, const Date& right);
friend int operator<(const Date& left, const Date& right);
};
#endif //DATE_H
However, the 5th line (static const int daysInMonth[] = {0, 31, 28,
31, 30, 31, 30, 31, 31, 30, 31, 30, 31} does not compile.
The error information is :
Date.h:5: error: a brace-enclosed initializer is not allowed here
before '{' token
Date.h:5: error: invalid in-class initialization of static data member
of non-integral type 'const int []'
This line is to save and provide the numbers of the days in the
months.
How could I implement what I want ?