file1:date_header.h
namespace Chrono
{
class Date
{
public://公共界面
enum Month{jan=1,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec};
........
Date(int dd=0,Month mm=Month(0),int yy=0);//0的意思是“取默认值”
.......
private:
int d,y; //表示
Month m;
static Date default_date;
};
}
file2:date_implement.cpp
#include"date_header.h"
#include<iostream>
using namespace std;
using namespace Chrono;
Date Date::default_date(22,feb,1901);//初始化static数据成员
Date::Date(int dd,Month mm,int yy)//构造函数
{.........}
...............
void Date::display(Date &d)const//显示日期
{
cout<<d.d<<" "<<d.m<<" "<<d.y<<endl;
}
file3:main.cpp
#include"date_header.h"
void main()
{
using namespace Chrono;
Date d;
Date::display(d);
}
编译错误:D:\programe\the c++ programming language\t\main.cpp(9) : error C2352: 'Chrono::Date::display' : illegal call of non-static member function
d:\programe\the c++ programming language\t\date_header.h(24) : see declaration of 'display'
请帮忙,谢谢,
我的想法是:在main中没有初始化date d,是想通过display()函数显示我开始设置的默认值Date Date::default_date(22,feb,1901);//初始化static数据成员
结果,就出现这个问题,请指教谢谢:)
构造函数是:
Date::Date(int dd,Month mm,int yy)//构造函数
{
if(yy==0)
yy=default_date.year();
if(mm==0)
mm=default_date.month();
if(dd==0)
dd=default_date.day();
int max;
switch(mm)
{
case feb: //对2月的处理,分闰年和平年
max=28+leapyear(yy);
break;
case apr: case jun: case sep: case nov:
max=30;
break;
case jan: case may: case jul: case aug: case oct: case dec:
max=31;
break;
default:
throw Bad_date();
}
if(dd<1||max<dd) //输入的日有问题
throw Bad_date();
y=yy;
m=mm;
d=dd;
}
file3:main.cpp
#include"date_header.h"
void main()
{
using namespace Chrono;
Date d;
Date::display(d); // 这句改为 d.display(d);只有静态函数可以这样调用。那需要把display函数定义为static。
}
编译错误:D:\programe\the c++ programming language\t\main.cpp(9) : error C2352: 'Chrono::Date::display' : illegal call of non-static member function
d:\programe\the c++ programming language\t\date_header.h(24) : see declaration of 'display'
请帮忙,谢谢,