怎么实现这个? [英] How to implement this?
问题描述
我正在编写这个小班级日期,代表日期
由年,月和日组成。
头文件如下:
#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();
静态布尔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) ;
朋友int op erator>(const日期& left,const Date&右边);
朋友int操作符<(const日期&左,const日期和右边);
};
# endif // DATE_H
然而,第5行(static const int daysInMonth [] = {0,31,28,
31,30,31,30,31, 31,30,31,30,31};)不编译。
错误信息是:
Date.h:5 :错误:这里不允许使用大括号括起初始化程序
'''''令牌之前
Date.h:5:错误:静态的类内初始化无效数据成员
非整数类型''const int []''
此行用于保存并提供
$中的天数b $ b个月。
我怎样才能实现我想要的东西?
xz写道:
然而,第5行(static const int daysInMonth [] = {0,31,28,
31,30,31,30 ,31,31,30,31,30,31};)不编译。
错误信息是:
Date.h:5:错误:此处不允许使用大括号括起初始化程序
''''令牌之前
Date.h:5:错误:静态数据成员的类内初始化无效
非整数类型''const int []''
此行用于保存并提供
个月的天数。
怎么可能我实现了我想要的东西?
你必须在你的
实现源文件(.cpp)中的类定义之外初始化daysInMonth,就像这样:
//在标题中
类日期
{
static const int daysInMonth [] ;
};
//在实现文件中(Date.cpp?)
const int Date :: daysInMonth [] = {0,31,28,31,30,31,30,31,31,
31,31,30,31};
-
Miguel Guedes
- X标志着垃圾邮件发送者的地位。如果您希望通过电子邮件与我联系,
从我的地址中删除X. -
Miguel Guedes写道:
xz写道:
>然而,第5行(static const int daysInMonth [] = {0,31,28,
31,30,31,30,31,31,30,31,30,31 };)不编译。
错误信息是:
Date.h:5:错误:此处不允许使用大括号括起初始化程序
''{''令牌
Date.h:5:错误:静态数据成员的类内初始化无效
非整数类型''const int []''
这行是为了保存并提供
月份的天数。
我怎样才能实现我想要的?
您必须在
实现源文件(.cpp)中的类定义之外初始化daysInMonth,如下所示:
BTW,这是如此因为静态成员的定义相当于
外部变量de结果 - 只有一个。
-
Miguel Guedes
- X标志着垃圾邮件发送者的地位。如果您希望通过电子邮件与我联系,
从我的地址中删除X. -
8月26日下午3:27,Miguel Guedes< miguel.a.gue ... @ gmailX.comwrote:
Miguel Guedes写道:
xz写道:
但是,第5行(static const int daysInMonth [] = {0,31,28,
31,30,31,30,31,31,30,31,30,31};)不编译。
错误信息是:
< blockquote class =post_quotes>
Date.h:5:错误:这里不允许使用大括号括起初始化程序
''{''之前令牌
Date.h:5:错误:静态数据成员的类内初始化无效
非整数类型''const int []''
此行用于保存并提供
$ b $中的天数b个月。
我怎样才能实现我想要的?
您必须在
实现源文件(.cpp)中的类定义之外初始化daysInMonth,像这样:
感谢您的回复
BTW,这是因为静态成员的定义相当于
外部变量定义 - 只有一个。
这也适用于静态成员函数,对吗?
例如...中定义的函数static bool isLeapYear(int year); "是
也像外部函数一样?
然而,我发现如果我有Date的实例,比如说,Date
date(...);
我无法通过
Date.isLeapYear(2000);
调用函数isLeapYear(int year)
相反,我可以通过
date.isLeapYear(2000)来调用它。
这对我来说很奇怪,因为isLeapYear(int)是静态的。第二个
方式调用它看起来像是theLeapYear是一个成员函数。
>
- -
Miguel Guedes
- X标志着垃圾邮件发送者的地位。如果您希望通过电子邮件与我联系,
从我的地址中删除X. -
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 ?
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. -
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.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. -
这篇关于怎么实现这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!