473,394 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to implement this?

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 ?

Aug 26 '07 #1
5 8100
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:

// 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};
--
Miguel Guedes

- X marks the spot for spammers. If you wish to get in touch with me by email,
remove the X from my address. -
Aug 26 '07 #2
Miguel Guedes wrote:
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:
BTW, this is so because the definition of static members is equivalent to an
external variable definition - there is only one.
--
Miguel Guedes

- X marks the spot for spammers. If you wish to get in touch with me by email,
remove the X from my address. -
Aug 26 '07 #3
xz
On Aug 26, 3:27 pm, Miguel Guedes <miguel.a.gue...@gmailX.comwrote:
Miguel Guedes wrote:
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.
>
--
Miguel Guedes

- X marks the spot for spammers. If you wish to get in touch with me by email,
remove the X from my address. -

Aug 26 '07 #4
"xz" <zh*********@gmail.comwrote in message
news:11**********************@g4g2000hsf.googlegro ups.com...
On Aug 26, 3:27 pm, Miguel Guedes <miguel.a.gue...@gmailX.comwrote:
>Miguel Guedes wrote:
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.
Aug 26 '07 #5

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.
Aug 26 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Billy Porter | last post by:
Greetings, I got a class that wraps the System.Data.SqlClient.SqlConnection class (no COM interaction). I'm not sure if I'm supposed to implement the IDisposable pattern for this wrapper or not....
4
by: Peter | last post by:
I want to copy a parent class instance's all datas to a child's. It's actually a C++'s copy constructor. But why the following code does not work - there is a compile error! How it should look...
13
by: Sherif ElMetainy | last post by:
Hello I was just got VS 2005 preview, and was trying generics. I tried the following code int intArray = new int; IList<int> intList = (IList<int>) intArray; it didn't compile, also the...
3
by: Brett Hall | last post by:
I have a VB.NET interface that my managed C++ code is to implement. I seem to be stuck implementing an event defined in that interface. Does anyone have a simple code snippet that will show me...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
1
by: Paul | last post by:
Hi all, I'm trying to implement IList and keep getting an error when trying to implement GetEnumerator(). My class has a List<String> and I've been using its methods as return types for IList,...
7
by: moondaddy | last post by:
If I'm in a class that inherits an interface, is there a shortcut key that will write the implementation of the interface into the class? I remember seeing something like this in vb.net. ...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
52
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member...
5
by: Tony Johansson | last post by:
Hello! Assume you have the following interface and classes shown below. It is said that a class must implement all the methods in the interface it inherits. Below we have class MyDerivedClass...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.