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==(cons t Date& left, const Date& right);
friend int operator!=(cons t 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 ? 5 8143
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::daysInMon th[]= {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. -
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. -
On Aug 26, 3:27 pm, Miguel Guedes <miguel.a.gue.. .@gmailX.comwro te:
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. -
"xz" <zh*********@gm ail.comwrote in message
news:11******** **************@ g4g2000hsf.goog legroups.com...
On Aug 26, 3:27 pm, Miguel Guedes <miguel.a.gue.. .@gmailX.comwro te:
>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::isLeapYea r(2000);
should also work.
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::isLeapYea r(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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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. Since one of it's members (SqlConnection)
implements this interface, I'm thinking maybe I ought to. But on the other
hand, those unmanaged resources has already been wrapped in the
SqlConnection class...
If so, how would my Dispose method look...
|
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 like?
(The background is I don't know (I don't care indeed) all members in
DataGrid, so I don't want to copy all members in DataGrid one by one.)
public class GridEx : DataGrid
{
public GridEx()
|
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 following didn't compile
|
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 the basics of what I need to
implement?
I've seen all the MSDN articles on implementing events in managed C++
and
I've gotten events to work without issue when implementing all the
constructs
|
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 having a problem in the calling
program. I searched online and found suggestions that I return an Array
instead so I modified my code (below) to return an Array instead of an
ArrayList. Now I get the message when I try to run just my webservice...
| |
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, but I can't seem to figure the the get enumerator
section. I try:
//IEnumerable
public IEnumerator GetEnumerator()
|
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.
Thanks.
--
moondaddy@nospam.nospam
|
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 magic, I
wrote the following module. It is mainly useful as a light weight tool
to help programmers catch mistakes at definition time (e.g., forgetting
to implement a method required by the given interface). This is handy
when unit tests or...
|
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
'UselessJunkForDissassembly.IInvocableInternals.OperationValidate(string)'
C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27):
error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not
implement interface member...
|
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 that inherits IMyInterface but
MyDerivedClass doesn't implement method DoSomething() it inherits it from
the base class MyBaseClass.
So the statement that a class must implement all method in an interface that
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
| |
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |