C++类指针数组初始化问题

Falleyes 2013-06-09 08:03:11
/*完整代码如下,问题附在程序最后*/

#include<iostream>
#include<iomanip>
using namespace std;

class Date
{
public:
Date *date;
/* 默认构造函数,以fullyear的形式给出年月日,默认值为1990年1月1日,同时设置日期分隔符为“-” */
Date(int years = 1990, int months = 1, int days = 1);
/* get、set方法 */
// 设置日期,如果有非法的月或日,将其置为1
void setDate(int years, int months, int days);
void setYear(int years);
int getYear() const;
void setMonth(int months);
int getMonth() const;
void setDay(int months);
int getDay() const;
void setSeparator(char separators);

/* 输出函数,请使用setfill(‘0’)和setw(2) */
void printFullYear() const; // 以YYYY-MM-DD的形式打印,2011-01-08
void printStandardYear() const; // 以YY-MM-DD的形式打印,比如11-01-08
/* 计算函数 */
// 计算当前日期与参数日期之间相差几个整年,仅考虑参数日期比当前日期晚的情况
int fullYearsTo(const Date &date) const;
/* 计算当前日期与参数日期之间相差多少天(考虑闰年),如果参数日期在当前日期之前,返回负数。 */
int operator-(const Date &date);
bool operator>(const Date & date);
bool operator<(const Date & date);
const Date & operator[](int i) const; //重载[]
/* 新增函数,可以被daysTo函数调用 */
int getDayOfYear() const; //计算当前日期是本年的第几天
int getLeftDaysYear() const; //计算当前日期距本年结束还有几天,不包括当前日期这一天
private:
int year;
int month;
int day;
char separator; // 日期分隔符
/* 新增数据成员和函数成员 */
/*声明静态常变量,每月的天数,在.cpp文件中定义并初始化 */
static const int DAYS_PER_MONTH[12];
/*根据年和月,判断参数日期是否合法。如果合法,返回day,否则返回1。*/
int checkDay(int days) const;
bool isLeapyear(int year) const;//断参数年是否是闰年。
};

const int Date::DAYS_PER_MONTH[12]={31,28,31,30,31,30,31,31,30,31,30,31};

Date::Date(int years,int months,int days)
{
setDate(years,months,days);
setSeparator('-');
}

void Date::setDate(int years,int months,int days)
{
setYear(years);
setMonth(months);
setDay(days);
}

void Date::setYear(int years)
{
if(!(years-(int)years)&&years>0)
year=years;
else
year=1; //非法输入置1
}

int Date::getYear() const
{
return year;
}

void Date::setMonth(int months)
{
if(!(months-(int)months)&&months>0&&months<13) //非法输入判断
month=months;
else
month=1;
}

int Date::getMonth() const
{
return month;
}

int Date::checkDay(int days) const //检查日期
{
if(!(days-(int)days)) //判断日期是否合法
{
if(month==2)
{
if(isLeapyear(year)) //判断闰年
if(days>0&&days<=DAYS_PER_MONTH[1]+1)
return days;
else
return 1;
}
else if(days>0&&days<=DAYS_PER_MONTH[month])
return days;
else
return 1;
}
else
return 1;
}

void Date::setDay(int days)
{
day=checkDay(days);
}

int Date::getDay() const
{
return day;
}

void Date::setSeparator(char separators)
{
separator=separators;
}

/* 输出函数,请使用setfill(‘0’)和setw(2) */
void Date::printFullYear() const // 以YYYY-MM-DD的形式打印,2011-01-08
{
cout<<year<<separator;
if(!(month/10))
cout<<setw(2)<<setfill('0')<<month<<separator;
else
cout<<month<<separator;
if(!(day/10))
cout<<setw(2)<<setfill('0')<<day<<endl;
else
cout<<day<<endl;
}

void Date::printStandardYear() const // 以YY-MM-DD的形式打印,比如11-01-08
{
cout<<year%100<<separator;
if(!(month/10))
cout<<setw(2)<<setfill('0')<<month<<separator; //用0填充
else
cout<<month<<separator;
if(!(day/10))
cout<<setw(2)<<setfill('0')<<day<<endl;
else
cout<<day<<endl;
}
/* 计算函数 */
// 计算当前日期与参数日期之间相差几个整年,仅考虑参数日期比当前日期晚的情况
int Date::fullYearsTo(const Date &date) const
{
if(date.month>month) //比较并输出整差值
return date.month-year;
else if(date.month==month)
if(date.day>day)
return date.year-year;
else
return date.year-year-1;
else
return date.year-year-1;
}
/* 计算当前日期与参数日期之间相差多少天(考虑闰年),如果参数日期在当前日期之前,返回负数。 */
int Date::operator-(const Date &date)
{
int days1=getDayOfYear(),days2=date.getDayOfYear(); //转换为年和日的计算
int dayto=0;
/*计算年差,和日差,最后统一为日差*/

if(*this>date)
{
for(int i=year-1;i>=date.year;i--)
if(isLeapyear(i))
dayto+=366;
else
dayto+=365;
dayto+=days1-days2;
}
else
{
for(int i=year;i<date.year;i++)
if(isLeapyear(i))
dayto+=366;
else
dayto+=365;
dayto+=days2-days1;
dayto*=-1; //*(-1)
}
return dayto;
}

bool Date::operator>(const Date & date) //重载>号
{
if(year>date.year)
return true;
if(year==date.year&&month>date.month)
return true;
if(year==date.year&&month==date.month&&day>date.day)
return true;
return false;
}

bool Date::operator<(const Date & date) //重载<号
{
if(year<date.year)
return true;
if(year==date.year&&month<date.month)
return true;
if(year==date.year&&month==date.month&&day<date.day)
return true;
return false;
}

const Date & Date::operator[](int i) const //重载[]
{
return date[i];
}

int Date::getDayOfYear() const //计算当前日期是本年的第几天
{
int days=0;
if(isLeapyear(year))
if(month>2)
days=1;
for(int i=0;i<month-1;i++)
days+=DAYS_PER_MONTH[i];
days+=day;
return days;
}

int Date::getLeftDaysYear() const //计算当前日期距本年结束还有几天,不包括当前日期这一天
{
int days=365-getDayOfYear();
if(isLeapyear(year))
return days+1;
else
return days;
}

bool Date::isLeapyear(int year) const //断参数年是否是闰年。
{
if(!(year%4)&&year%100||!(year%400)) //是闰年返回真
return true;
else
return false;
}

//雇员类
class Employee
{
public:
//构造函数,使用“成员初始化器”初始化数据成员
Employee(const string first, const string last, const Date &births, const Date &hires);
//打印员工的信息。调用Date类的print函数,打印员工的生日和雇佣日期。
void print() const;
//计算员工在参数指定的日期时,满多少岁。请使用Date类的fullYearsTo函数
int getAge(Date& date) const{return birthDate.fullYearsTo(date.getYear());}
//计算该员工在参数指定的日期时,工作满了多少年。
int getYearsWorked(Date& date) const{return hireDate.fullYearsTo(date.getYear());}
//计算该员工在参数指定的日期时,工作了多少天。使用Date类的daysTo函数。
int getDaysWorked(Date& date) const{return date-hireDate;}
~Employee(){}; //析构函数
static const Employee & getMostFaith(const Employee employees[], int n); //工龄最长成员
Date GetHireDate(){return hireDate;}
void setHireDate(Employee & e) //重新设置雇佣日期
{hireDate.setYear(e.GetHireDate().getYear());hireDate.setMonth(e.GetHireDate().getMonth());hireDate.setDay(e.GetHireDate().getDay());}
private:
string firstName;
string lastName;
/* 要求:出生日期、雇用日期必须声明为const,因为对于每个雇员,这两个属性都是不会变化的。*/
Date birthDate; //内嵌对象,出生日期
Date hireDate; //内嵌对象,雇用日期
};

Employee::Employee(const string first, const string last, const Date &births, const Date &hires):firstName(first),lastName(last)
{
birthDate.setYear(births.getYear());
birthDate.setMonth(births.getMonth());
birthDate.setDay(births.getDay());
hireDate.setYear(hires.getYear());
hireDate.setMonth(hires.getMonth());
hireDate.setDay(hires.getDay());
}
//打印员工的信息。调用Date类的print函数,打印员工的生日和雇佣日期。
void Employee::print() const
{
cout<<"Hired:";
hireDate.printFullYear();
cout<<"Birthday:";
birthDate.printFullYear();
}

static const Employee & getMostFaith(Employee employees[], int n) //工龄最长成员
{
int min=0;
for(int i=1;i<n;i++)
{
if(employees[i].GetHireDate()<employees[min].GetHireDate())
min=i; //比较最小并记录数组位置
}
return employees[min];
}

void main()
{
Date birth(1969, 8, 11);
Date hire(1998, 4, 1);
Date today(2010, 4, 30);
Employee manager("Bob", "Blue",birth, hire);
cout<<endl;
manager.print(); //Blue, Bob Hired: 1998-04-01 Birthday: 1969-08-11
cout<<endl;
cout<<manager.getAge(today) << endl; //40 // 工作满了40年
cout<<manager.getDaysWorked(today) <<endl<<endl; //4412 // 已工作了4412天

Date *birth[5]={new Date(1993,12,10),new Date(1992,10,31),new Date(1989,12,31),new Date(1994,1,2),new Date(1993,5,6)}; //所需的2组5元Date类数据
Date *hire[5]={new Date(2010,3,5),new Date(2009,4,1),new Date(2003,6,4),new Date(2009,12,1),new Date(2010,3,1)};


/*const Date birth1(1993,12,10); //所需的Date类数据
const Date birth2(1992,10,31);
const Date birth3(1989,12,31);
const Date birth4(1994,1,2);
const Date birth5(1993,5,6);
const Date hire1(2010,3,5);
const Date hire2(2009,4,1);
const Date hire3(2003,6,4);
const Date hire4(2009,12,1);
const Date hire5(2010,3,1);*/


Employee employees[5]= //初始化5个雇员
{
Employee("甲","某",birth[0],hire[0]),
Employee("乙","某",birth[1],hire[1]),
Employee("丙","某",birth[2],hire[2]),
Employee("丁","某",birth[3],hire[3]),
Employee("戊","某",birth[4],hire[4])
};

for(int i=0;i<5;i++) //打印这五个雇员的信息
{
cout<<"雇员 #"<<i+1;
employees[i].print();
cout<<"已经工作了"<<employees[i].getDaysWorked(today)<<"天"<<endl; //打印工作天数
}
cout<<"工龄最长的是信息:\n";
getMostFaith(employees,5).print(); //打印工龄最长的雇员信息
cout<<endl;
}

红色部分代码提示:error C2040: “birth”:“Date *[5]”与“Date”的间接寻址级别不同。
请教高手,这里应该怎么写呢?
...全文
156 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Falleyes 2013-06-10
  • 打赏
  • 举报
回复
引用 1 楼 ananluowei 的回复:
已经定义 Date birth(1969, 8, 11); 还定义 Date *birth[5]
就这个问题,谢谢拉~~~
Falleyes 2013-06-10
  • 打赏
  • 举报
回复
引用 3 楼 hugett 的回复:
birth跟hire重定义了。。main函数开始那里已经定义了这两个变量。。改个名字就可以了。。
哦,明白了,非常感谢~~~
Falleyes 2013-06-10
  • 打赏
  • 举报
回复
引用 2 楼 ananluowei 的回复:
再说定义一维数组也不是你这么定义的 Date *birth[5]定义的是二维数组。
哦,我这里定义的是一维指针数组,每个指针对应一个对象
我看你有戏 2013-06-10
  • 打赏
  • 举报
回复
数组定义没问题啊
许安培 2013-06-10
  • 打赏
  • 举报
回复
怎么运行后,出现停止工作呢?求指点?
hugett 2013-06-09
  • 打赏
  • 举报
回复
birth跟hire重定义了。。main函数开始那里已经定义了这两个变量。。改个名字就可以了。。
大尾巴猫 2013-06-09
  • 打赏
  • 举报
回复
再说定义一维数组也不是你这么定义的 Date *birth[5]定义的是二维数组。
大尾巴猫 2013-06-09
  • 打赏
  • 举报
回复
已经定义 Date birth(1969, 8, 11); 还定义 Date *birth[5]

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧