How to implement this?

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 ?
 
M

Miguel Guedes

xz said:
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 ?

You must initialize daysInMonth outside the class definition in your
implementation source file (.cpp), like so:

// In header
class Date
{
static const int daysInMonth[];
};

// In implementation file (Date.cpp?)
const int Date::daysInMonth[]= {0, 31, 28, 31, 30, 31, 30, 31, 31,
30, 31, 30, 31};
 
M

Miguel Guedes

Miguel said:
xz said:
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 ?

You must initialize daysInMonth outside the class definition in your
implementation source file (.cpp), like so:

BTW, this is so because the definition of static members is equivalent to an
external variable definition - there is only one.
 
X

xz

Miguel said:
xz said:
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 ?
You must initialize daysInMonth outside the class definition in your
implementation source file (.cpp), like so:
Thanks for your reply
BTW, this is so because the definition of static members is equivalent to an
external variable definition - there is only one.
And this also holds for the static member functions, right?
e.g. the function defined in " static bool isLeapYear(int year); " is
also like an external function?

However, I found that if I have a instance of Date, say, Date
date(...);
I cannot call the function isLeapYear(int year) by
Date.isLeapYear(2000);

But instead, I can call it by
date.isLeapYear(2000);

This looks strange for me since isLeapYear(int) is static. The second
way to call it looks like that isLeapYear is a member function.
 
J

Jim Langston

xz said:
Miguel said:
xz wrote:
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 ?
You must initialize daysInMonth outside the class definition in your
implementation source file (.cpp), like so:
Thanks for your reply
BTW, this is so because the definition of static members is equivalent to
an
external variable definition - there is only one.
And this also holds for the static member functions, right?
e.g. the function defined in " static bool isLeapYear(int year); " is
also like an external function?

However, I found that if I have a instance of Date, say, Date
date(...);
I cannot call the function isLeapYear(int year) by
Date.isLeapYear(2000);

But instead, I can call it by
date.isLeapYear(2000);

This looks strange for me since isLeapYear(int) is static. The second
way to call it looks like that isLeapYear is a member function.

Date::isLeapYear(2000);

should also work.
 
D

Duane Hebert

However, I found that if I have a instance of Date, say, Date
date(...);
I cannot call the function isLeapYear(int year) by
Date.isLeapYear(2000);

How about Date::isLeapYear(2000);
You can't use the dot operator with a type.
But instead, I can call it by
date.isLeapYear(2000);

Sure. You can use an instance of the class
but you don't need an instance.
This looks strange for me since isLeapYear(int) is static. The second
way to call it looks like that isLeapYear is a member function.

A static function can't access the internal "this" pointer.
It doesn't mean that it's not a member.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,134
Latest member
Lou6777736
Top