CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
英特尔®游戏设计大赛100美元现金周周送 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C++ 语言

看看这程序,VC6.0输出的时间很奇怪啊(及时结贴)

楼主boyplayee()2003-12-03 15:55:23 在 C/C++ / C++ 语言 提问

#include   <iostream.h>    
  #include   <string.h>  
  #include   <stdio.h>  
  typedef   char   string80[80];  
   
  class   Date  
  {  
  public:  
    Date()   {}  
    Date(int   y,   int   m,   int   d)   {   SetDate(y,   m,   d);   }  
    void   SetDate(int   y,   int   m,   int   d)  
  {  
    Year   =   y;  
    Month   =   m;  
    Day   =   d;  
  }  
  GetStringDate(string80   &Date)  
  {  
    sprintf(Date,   "%d/%d/%d",   Year,   Month,   Day);  
  }  
  protected:  
    int   Year,   Month,   Day;  
  };  
   
  class   Time  
  {  
  public:  
  Time()   {}  
  Time(int   h,   int   m,   int   s)   {   SetTime(h,   m,   s);   }  
    SetTime(int   h,   int   m,   int   s)  
  {  
  Hours   =   h;  
  Minutes   =   m;  
  Seconds   =   s;  
  }  
    GetStringTime(string80   &Time)  
  {  
    sprintf(Time,   "%d:%d:%d",   Hours,   Minutes,   Seconds);  
  }  
  protected:  
  int   Hours,   Minutes,   Seconds;  
  };  
   
  class   TimeDate:public   Date,   public   Time  
  {  
  public:  
  TimeDate():Date()   {}  
  TimeDate(int   y,   int   mo,   int   d,   int   h,   int   mi,   int   s):Date(y,   mo,   d),Time(h,   mi,   s)   {}  
  GetStringDT(string80   &DTstr)  
  {  
  sprintf(DTstr,   "%d/%d/%d;%d:%d:%d",   Year,   Month,   Day,   Hours,   Minutes,   Seconds);  
  }  
  };  
   
  void   main()  
  {  
  TimeDate   date1,   date2(1998,   8,   12,   12,   45,   10);  
  string80   DemoStr;  
  date1.SetTime(10,   30,   45);  
  date1.SetDate(1998,   8,   7);  
  date1.GetStringDT(DemoStr);  
  cout<<"The   date1   date   and   time   is:"<<date1.GetStringDate(DemoStr)<<endl;  
  cout<<"The   date1   date   is:"<<date1.GetStringTime(DemoStr)<<endl;  
  //cout<<"The   date1   time   is:"<<date2.GetStringDT(DemoStr)<<endl;  
  cout<<"The   date2   date   and   time   is:"<<   date2.GetStringDT(DemoStr)<<endl;  
  }  
   
   
  这会奇怪了,输出居然是:  
  The   date1   date   and   time   is:18  
  The   date1   date   is:8  
  The   date2   date   and   time   is:18  
  根本没有18,8,18啊,错在哪而啊?  
   
  若该成:  
  #include   <iostream>    
  #include   <string>  
  #include   <stdio>  
  using   namespace   std;  
  typedef   char   string80[80];  
   
  class   Date  
  {  
  public:  
    Date()   {}  
    Date(int   y,   int   m,   int   d)   {   SetDate(y,   m,   d);   }  
    void   SetDate(int   y,   int   m,   int   d)  
  {  
    Year   =   y;  
    Month   =   m;  
    Day   =   d;  
  }  
  VOID   GetStringDate(string80   &Date)  
  {  
    sprintf(Date,   "%d/%d/%d",   Year,   Month,   Day);  
  }  
  protected:  
    int   Year,   Month,   Day;  
  };  
   
  class   Time  
  {  
  public:  
  Time()   {}  
  Time(int   h,   int   m,   int   s)   {   SetTime(h,   m,   s);   }  
  void   SetTime(int   h,   int   m,   int   s)  
  {  
  Hours   =   h;  
  Minutes   =   m;  
  Seconds   =   s;  
  }  
  VOID   GetStringTime(string80   &Time)  
  {  
    sprintf(Time,   "%d:%d:%d",   Hours,   Minutes,   Seconds);  
  }  
  protected:  
  int   Hours,   Minutes,   Seconds;  
  };  
   
  class   TimeDate:public   Date,   public   Time  
  {  
  public:  
  TimeDate():Date()   {}  
  TimeDate(int   y,   int   mo,   int   d,   int   h,   int   mi,   int   s):Date(y,   mo,   d),Time(h,   mi,   s)   {}  
  VOID   GetStringDT(string80   &DTstr)  
  {  
  sprintf(DTstr,   "%d/%d/%d;%d:%d:%d",   Year,   Month,   Day,   Hours,   Minutes,   Seconds);  
  }  
  };  
   
  int   main()  
  {  
  TimeDate   date1,   date2(1998,   8,   12,   12,   45,   10);  
  string80   DemoStr;  
  date1.SetTime(10,   30,   45);  
  date1.SetDate(1998,   8,   7);  
  date1.GetStringDT(DemoStr);  
  cout<<"The   date1   date   and   time   is:"<<date1.GetStringDate(DemoStr)<<endl;  
  cout<<"The   date1   date   is:"<<date1.GetStringTime(DemoStr)<<endl;  
  //cout<<"The   date1   time   is:"<<date2.GetStringDT(DemoStr)<<endl;  
  cout<<"The   date2   date   and   time   is:"<<   date2.GetStringDT(DemoStr)<<endl;  
  }  
  这回VC6.0里通不过了,  
  Compiling...  
  DateTime.cpp  
  D:\C++\DateTime.cpp(62)   :   error   C2679:   binary   '<<'   :   no   operator   defined   which   takes   a   right-hand   operand   of   type   'void'   (or   there   is   no   acceptable   conversion)  
  D:\C++\DateTime.cpp(63)   :   error   C2679:   binary   '<<'   :   no   operator   defined   which   takes   a   right-hand   operand   of   type   'void'   (or   there   is   no   acceptable   conversion)  
  D:\C++\DateTime.cpp(65)   :   error   C2679:   binary   '<<'   :   no   operator   defined   which   takes   a   right-hand   operand   of   type   'void'   (or   there   is   no   acceptable   conversion)  
  Error   executing   cl.exe.  
   
  DateTime.exe   -   3   error(s),   0   warning(s)  
  问题点数:10、回复次数:1Top

1 楼plainsong(短歌)()回复于 2003-12-03 16:21:04 得分 10

第一段的问题是:  
  你的GetStringDT等函数都定义为int的返回值(未定义返回值类型则认为是int,这是VC不符合标准的地方,其实应该给出编译错误),而你没有返回任何数据(也应该给出错误),所以返回值是随机的。而你的<<data1.Get...就把一个随机的整数输出出来了。  
  第二段中这个函数的返回值是void,而void类型是无法输出的。  
   
  我想你的意思应该:  
  string80&   GetStringDate(string80   &Date)  
  {  
    sprintf(Date,   "%d/%d/%d",   Year,   Month,   Day);  
    return   Data;  
  }  
  下同。  
  Top

相关问题

  • 为什么这段程序输出这个?好奇怪。
  • 奇怪,简单的string在vc中输出不了?
  • vc中gui程序要开cui输出,怎么办?
  • 奇怪的问题,这段程序怎么什么都没有输出??????????????????
  • VC的HelloWorld的程序的问题,奇怪!请大家指教!
  • 求程序输出结果
  • 程序输出的问题
  • 奇怪的程序
  • 一个很菜的问题,用vc创建的一个单文档的程序,怎么输出数字
  • VC高手过来看看,关于cmd提示信息输出?实现方案和实例程序给分!

关键词

  • date
  • timedate
  • dtstr
  • getstringdt
  • setdate
  • demostr
  • settime
  • minutes
  • hours
  • sprintf

得分解答快速导航

  • 帖主:boyplayee
  • plainsong

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo