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”的间接寻址级别不同。
请教高手,这里应该怎么写呢?
...全文
157 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]
目 录 序言 前言 第1章 程序设计与算法 1 1.1 程序设计语言的发展 1 1.2 C语言的特点 2 1.2.1 C语言是中级语言 2 1.2.2 C语言是结构化语言 3 1.2.3 C语言是程序员的语言 3 1.3 C语言的程序结构 4 1.3.1 基本程序结构 4 1.3.2 函数库和链接 6 1.3.3 开发一个C程序 7 1.3.4 C语言的关键字 7 1.4 算法 8 1.4.1 流程图与算法的结构化描述 9 1.4.2 用N-S图描述算法 12 1.4.3 用PAD图描述算法 13 第2章 数据类型、运算符和表达式 14 2.1 C语言的数据类型 14 2.2 常量与变量 15 2.2.1 标识符命名 15 2.2.2 常量 16 2.2.3 变量 16 2.3 整型数据 16 2.3.1 整型常量 16 2.3.2 整型变量 17 2.4 实型数据 18 2.4.1 实型常量 18 2.4.2 实型变量 18 2.5 字符型数据 19 2.5.1 字符常量 19 2.5.2 字符串常量 19 2.5.3 转义字符 20 2.5.4 符号常量 20 2.5.5 字符变量 21 2.6 运算符 22 2.6.1 算术运算符 22 2.6.2 自增和自减 22 2.6.3 关系和逻辑运算符 23 2.6.4 位操作符 24 2.6.5 ?操作符 26 2.6.6 逗号操作符 27 2.6.7 关于优先级的小结 27 2.7 表达式 28 2.7.1 表达式中的类型转换 28 2.7.2 构成符cast 29 2.7.3 空格与括号 29 2.7.4 C语言中的简写形式 29 第3章 程序控制语句 31 3.1 程序的三种基本结构 31 3.2 数据的输入与输出 31 3.2.1 scanf()函数 31 3.2.2 printf()函数 33 3.2.3 getchar()函数与putchar()函数 36 3.2.4 程序应用举例 37 3.3 条件控制语句 38 3.3.1 if 语句 38 3.3.2 switch 语句 43 3.3.3 程序应用举例 45 3.4 循环控制语句 46 3.4.1 while语句 47 3.4.2 do... while 语句 49 3.4.3 for 语句 50 3.4.4 break与continue语句 53 3.4.5 程序应用举例 54 第4章 函数 57 4.1 函数说明与返回值 57 4.1.1 函数的类型说明 57 4.1.2 返回语句 58 4.2 函数的作用域规则 60 4.2.1 局部变量 60 4.2.2 全局变量 61 4.2.3 动态存储变量 62 4.2.4 静态存储变量 63 4.3 函数的调用与参数 63 4.3.1 形式参数与实际参数 64 4.3.2 赋值调用与引用调用 64 4.4 递归 64 4.5 实现问题 66 4.5.1 参数和通用函数 66 4.5.2 效率 66 4.6 函数库和文件 67 4.6.1 程序文件的大小 67 4.6.2 分类组织文件 67 4.6.3 函数库 67 4.7 C语言的预处理程序与注释 67 4.7.1 C语言的预处理程序 68 4.7.2 #define 68 4.7.3 #error 69 4.7.4 # include 69 4.7.5 条件编译命令 70 4.7.6 #undef 72 4.7.7 #line 73 4.7.8 #pragma 73 4.7.9 预定义的宏名 73 4.7.10 注释 73 4.8 程序应用举例 74 第5章 数组 78 5.1 一维数组 78 5.1.1 向函数传递一维数组 78 5.1.2 字符串使用的一维数组 79 5.2 二维数组 80 5.2.1 二维数组的一般形式 80 5.2.2 字符串数组 84 5.3 多维数组 85 5.4 数组初始化 85 5.4.1 数组初始化 85 5.4.2 变长数组初始化 86 5.5 应用程序举例 87 第6章 指针 91 6.1 指针指针变量 91 6.2 指针变量的定义与引用 92 6.2.1 指针变量的定义 92 6.2.2 指针变量的引用 93 6.3 指针运算符与指针表达式 94 6.3.1 指针运算符与指针表达式 94 6.3.2 指针变量作函数的参数 95 6.4 指针数组 96 6.4.1 指针与一维数组 97 6.4.2 指针与二维数组 99 6.4.3 数组指针作函数的参数 102 6.4.4 指针与字符数组 108 6.5 指针的地址分配 111 6.6 指针数组 112 6.7 指向指针指针 118 6.8 main函数的参数 121 第7章 结构体与共用体 125 7.1 结构体类型变量的定义和引用 125 7.1.1 结构体类型变量的定义 126 7.1.2 结构体类型变量的引用 127 7.1.3 结构体类型变量的初始化 127 7.2 结构体数组的定义和引用 129 7.3 结构体指针的定义和引用 135 7.3.1 指向结构体类型变量的使用 135 7.3.2 指向结构体类型数组指针的 使用 136 7.4 链表的建立、插入和删除 138 7.4.1 单链表 139 7.4.2 单链表的插入与删除 141 7.5 共用体 149 7.5.1 共用体的定义 149 7.5.2 共用体变量的引用 150 第8章 输入、输出和文件系统 153 8.1 缓冲文件系统 153 8.1.1 文件的打开与关闭 153 8.1.2 文件的读写 155 8.1.3 随机读写文件 163 8.2 非缓冲文件系统 166 8.3 文件系统应用举例 167 第9章 实用编程技巧 170 9.1 图形应用技巧 170 9.1.1 显示适配器类型的自动测试 170 9.1.2 屏幕图像的存取技巧 179 9.1.3 屏幕显示格式的控制方法 181 9.1.4 使图形软件脱离BGI的方法 182 9.1.5 拷贝屏幕图形的方法 183 9.1.6 随意改变VGA显示器显示颜色的 技巧 185 9.1.7 用随机函数实现动画的技巧 187 9.1.8 用putimage 函数实现动画的技巧 189 9.2 菜单设计技术 191 9.2.1 下拉式菜单的设计 191 9.2.2 选择式菜单的设计 194 9.2.3 实现阴影窗口的技巧 195 9.3 音响技巧 197 9.3.1 音乐程序设计 197 9.3.2 自动识谱音乐程序 200 9.3.3 实现后台演奏音乐的技巧 203 第10章 C++入门 205 10.1 面向对象的概念 205 10.1.1 面向对象的程序结构 205 10.1.2 C++的类 206 10.2 C++的输入与输出 207 10.3 类与对象 208 10.3.1 类的定义与对象的引用 209 10.3.2 构造函数与析构函数 211 10.3.3 函数重载 215 10.3.4 友元 216 10.4 对象指针 219 10.5 派生类与继承类 225 10.5.1 单继承的派生类 225 10.5.2 多继承的派生类 233 附录A 常用字符与ASCII代码对照表 238 附录B 习题 239

64,666

社区成员

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

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