注册 登录
编程论坛 C++教室

这个错误不明白,请指教

二十七划生 发布于 2008-10-05 10:25, 680 次点击
程序代码如下:
#include<iostream.h>
#include<stdlib.h>
class Date{
      public:
             void setDate(int y,int m,int d);
             void showDate();
      private:
              int year;
              int month;
              int day;
      };
void Date::setDate(int y,int m,int d)
{
     year=y;
     month=m;
     day=d;
}
inline void Date::showDate()
{
       cout<<year<<"."<<month<<"."<<day<<endl;
}

int main()
{
     Date date1,date2,*date3;
     cout<<"Date1 set and output:"<<endl;
     date1.setDate(1998,4,28);
     date1.showDate();
     cout<<"Date2 set and output:"<<endl;
     date2.setDate(2002,11,14);
     date2.showDate();
     cout<<"Date3 set and output:"<<endl;
     date3->setDate(1998,10,25);
     date3->showDate();
     system("pause");
}
 程序命名为“类和对象”运行后出现下面这个对话框:


哪里出错了啊?请高手指教
6 回复
#2
blueboy820062008-10-05 12:08
兄弟,
*date3 不是那么用的...
#3
二十七划生2008-10-05 12:09
那该怎么用啊
#4
blueboy820062008-10-05 12:11
你这个对象的指针,指向一个存在的有效的对象了吗?
date3->setDate(1998,10,25);
date3->showDate();
这两句又是在操作谁啊?
#5
blueboy820062008-10-05 12:16
这样试一试:

Date date;
Date *date3=&date;
cout<<"Date3 set and output:"<<endl;
date3->setDate(1998,10,25);
date3->showDate();
#6
二十七划生2008-10-05 15:53
谢谢各位!
问题解决了
楼上该把"*"去掉
#7
blueboy820062008-10-05 17:26
[bo][un]二十七划生[/un] 在 2008-10-5 15:53 的发言:[/bo]
楼上该把"*"去掉

去掉,与前面两个对象不就一样了吗?还以为你想作指针呢...
1