我写了个关于处理时间的程序,能够在VC6下编译通过并运行良好,而当我用g++编译是老出错,不知道怎么回事,请大家指点。
欢迎大家对我的程序中存在的问题予以指正,感激不尽!
代码如下:
1#include<fstream>
2#include<string>
3#include<vector>
4#include<iostream>
5#include<assert.h>
6
7using namespace std;
8
9class time
10{
11 private:
12 int _hour; //小时
13 int _minute; //分钟
14 int _second; //秒
15 public:
16 time(); //默认构造函数
17 time(int hour,int minute,int second); //直接赋值
18 time(const time &time_object); //copy构造函数
19 ~time(); //析构函数
20
21 //赋值
22 time operator = (const time &time_object);
23 //用于读取类里面的数据
24 int get_hour()
25 {return _hour;}
26 int get_minute()
27 {return _minute;}
28 int get_second()
29 {return _second;}
30 //用于修改私有数据,或者赋值
31 int modify_hour(int hour)
32 {
33 _hour=hour;
34 return _hour;
35 }
36 int modify_minute(int minute)
37 {
38 _minute=minute;
39 return _minute;
40 }
41 int modify_second(int second)
42 {
43 _second=second;
44 return _second;
45 }
46
47 //输出,操作符<<重载,以打印到输出文件
48 friend ostream& operator << (ostream& ofile,const time &time_object); //输出<<重载
49 //输入,操作符>>重载,以将文件中的数据读入到time型的对象中
50 friend istream& operator >> (istream& ifile,const time &time_object);
51
52 friend time
53 operator + (const time &time_object1,const time &time_object2);
54
55 friend time&
56 operator += (time &time_object1,const time &time_object2);
57
58};
59
60//time.cpp..
61
62time::time() //默认构造函数
63 :_hour(0),_minute(0),_second(0)
64{
65
66}
67
68 //直接赋值
69time::time(int hour,int minute,int second)
70{
71 _hour=hour;
72 _minute=minute;
73 _second=second;
74}
75
76 //copy构造函数
77time::time(const time &time_object)
78{
79 _hour=time_object._hour;
80 _minute=time_object._minute;
81 _second=time_object._second;
82}
83
84time::~time()
85{ }
86
87
88time& operator += (time &time_object1,const time &time_object2)
89{
90 time_object1._hour+=time_object2._hour;
91 time_object1._minute+=time_object2._minute;
92 time_object1._second+=time_object2._second;
93 //以下
94 if(time_object1._second>=60)
95 {
96 time_object1._minute+=time_object1._second/60;
97 time_object1._second=time_object1._second%60;
98 }
99
100 if(time_object1._minute>=60)
101 {
102 time_object1._hour+=time_object1._minute/60;
103 time_object1._minute=time_object1._minute%60;
104 }
105
106 return time_object1;
107}
108
109
110time operator + (const time &time_object1,const time &time_object2)
111{
112 time result(time_object1);
113 result+=time_object2;
114 return result;
115}
116
117//赋值
118time time::operator = (const time &time_object)
119{
120 if(this!=&time_object)
121 {
122 _hour=time_object._hour;
123 _minute=time_object._minute;
124 _second=time_object._second;
125 }
126
127 return(*this);
128}
129
130ostream& operator << (ostream& ofile,const time& time_object)
131{
132 ofile<<time_object._hour<<":"<<time_object._minute<<":"<<time_object._second<<" ";
133 return ofile;
134}
135
136
137istream& operator >> (istream& ifile,time &time_object)
138{
139 long int before_label; //符号“:”前面的的部分
140 char after_label1,after_label2; //符号“:”后面的部分
141 char ch; //用于检验输入的字符
142
143 //sentry 保护I/O
144 std::istream::sentry the_sentry(ifile,true);
145
146 if(the_sentry)
147 {
148 if(ifile.bad())
149 return(ifile);
150
151 //获取空白之后的数值
152 ifile>>before_label;
153 time_object.modify_hour(before_label);
154
155 if(ifile.bad())
156 return(ifile);
157
158 ifile>>ch; //获取数值之后的第一个字符
159
160 if(ifile.bad())
161 return(ifile);
162
163 //等待“:”
164 if(ch==':')
165 {
166 ifile>>after_label1>>after_label2;
167 time_object.modify_minute((after_label1-'0')*10+after_label2);
168 }
169 else
170 cout<<"can't read the time file of minute"<<endl;
171
172 ifile>>ch; //第二次获取数值之后的第一个字符
173
174 if(ifile.bad())
175 return(ifile);
176
177 //等待“:”
178 if(ch==':')
179 {
180 ifile>>after_label1>>after_label2;
181 time_object.modify_second((after_label1-'0')*10+after_label2);
182 }
183 else
184 cout<<"can't read the time file of second"<<endl;
185
186 }
187 else
188 {
189 ifile.setstate(std::ios::failbit);
190 }
191 return(ifile);
192}
193
194int main()
195{
196 time time_object0;
197 cout<<"time_object0:"<<time_object0<<endl;
198 time time_object1(12,11,10);
199 cout<<"time_object1:"<<time_object1<<endl;
200 time time_object2(time_object1);
201 cout<<"time_object2:"<<time_object2<<endl;
202 time time_object3(11,8,11);
203 cout<<"time_object3:"<<time_object3<<endl;
204 time_object3+=time_object2;
205 cout<<"time_object3/2:"<<time_object3<<endl;
206 time time_object4;
207 time_object4=time_object1+time_object3;
208 cout<<"time_object4:"<<time_object4<<endl;
209
210 return 0;
211}
212
在GNU下编译信息如下: