473,320 Members | 1,821 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,320 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 8092
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.