看看这程序,VC6.0输出的时间很奇怪啊(及时结贴)
#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




