返回信息流程序如下,VC6下编译通过,但链接时出错,我怀疑错误是:static Date default_date
class Date{
public:
//...
static Date default_date;
};
ps: 我想先搜索来着,可是不知道该搜什么关键字,新手,只好麻烦大家
#include <iostream>
using namespace std;
class Date{
public:
enum Month {jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec};
class Bad_date{}; // 异常类
Date(int dd = 0, Month mm = Month(0), int yy = 0); // 0的意思就是“取默认值”
// 检查Date的函数
int day()const;
Month month()const;
int year()const;
std::string string_rep()const; //字符串表示
void char_rep(char s[])const; //n C风格的字符串表示
static void set_default(int, Month, int);
// 修改Date的函数
Date& add_year(int n); // 加n年
Date& add_month(int n); // 加n月
Date& add_day(int n); // 加n天
private:
int d, m, y;
static Date default_date;
// using namespace std;
};
int leap_year(int year)
{
int leap;
leap = (year%(year%100 ? 4: 400)) ? 0 : 1; /* year%4 == 0 && year%100 != 0 || year%400 == 0; */
return leap;
}
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:
max = 28 + leap_year(yy);
break;
case apr: case jun: case sep: case nov:
max = 30;
break;
case jan: case mar: 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;
}
inline int Date::day()const{
return d;
}
inline Date::Month Date::month()const{
return Month(m);
}
inline int Date::year()const{
return y;
}
std::string Date::string_rep()const{return NULL;}
void Date::char_rep(char s[])const{};
void Date::set_default(int d, Month m, int y)
{
Date::default_date = Date(d, m, y);
}
Date& Date::add_year(int n)
{
y += n;
return *this;
}
Date& Date::add_month(int n)
{
if(n == 0) return *this;
if(n > 0){
int delta_y = n / 12;
int mm = m + n % 12;
if(12 < mm){ // 注意:int(dec) = 12;
delta_y++;
mm -= 12;
}
// 处理Month(mm)没有天数d的情况
y += delta_y;
m = Month(mm);
return *this;
}
// 处理负数n
return *this;
}
Date& Date::add_day(int n)
{
return *this;
}
int main()
{
Date d;
cout << "hello, world" << endl;
return 0;
}
这是一条镜像帖。来源:北邮人论坛 / cpp / #28394同步于 2009/9/11
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
使用静态成员时出错
michealyao
2009/9/11镜像同步12 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
#include <iostream>
using namespace std;
class Date
{
public:
Date(int,int,int);
static Date* CreatInstance();
void print();
private:
Date();
int d, m, y;
static Date* default_date;
// using namespace std;
};
Date* Date::default_date=0;
Date* Date::CreatInstance()
{
if(default_date==0)
{
default_date=new Date(1,1,2009);
}
return default_date;
}
Date::Date(int day,int month,int year)
{
d=day;
m=month;
y=year;
}
void Date::print()
{
cout<<y<<":"<<m<<":"<<d<<endl;
}
int main()
{
Date* d;
d=Date::CreatInstance();
d->print();
cout << "hello, world" << endl;
cin.get();
return 0;
}
你这个设计有问题。
static成员的含义是独立于任何一个对象存在,所有类对象共有。
而你的程序里,Date类里包含一个静态Date对象default_date,要用这个static成员就必须实例化这个对象,而static对象应该独立于任何类对象,这就形成一个悖论。所以不能这么写
明白,The C++ Programming Language里这么写的,所以不敢确定
【 在 jokerlee 的大作中提到: 】
: 你这个设计有问题。
: static成员的含义是独立于任何一个对象存在,所有类对象共有。
: 而你的程序里,Date类里包含一个静态Date对象default_date,要用这个static成员就必须实例化这个对象,而static对象应该独立于任何类对象,这就形成一个悖论。所以不能这么写
第10章类 10.3节
【 在 hman 的大作中提到: 】
: 恩?
: The C++ Programming Language 这么写的? 第几章 哪里?