| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 755 人关注过本帖
标题:此程序在错在哪里
只看楼主 加入收藏
zjl138
Rank: 1
等 级:新手上路
威 望:1
帖 子:788
专家分:0
注 册:2007-11-12
收藏
 问题点数:0 回复次数:10 
此程序在错在哪里
//f0806.cpp
//Time类的operator++应用
#include<iostream>
#include<iomanip>
using namespace std;
class Time{
int hour,minute,second;
public:
void set(int h,int m,int s)
{hour=h;minute=m;second=s;}
friend Time&operator++(Time& a);
friend Time operator++(Time& a,int);
friend ostream& operator<<(ostream& o,const Time& t);
};
Time& operator++(Time& a)
{
if(!(a.second=(a.second+1)%60)&&!(a.minute=(a.minute+1)%60))
a.hour=(a.hour+1)%24;
return a;
}
Time operator++(Time& a,int )
{Time t(a);
if(!(a.second=(a.second+1)%60)&&!(a.minute=(a.minute+1)%60))
a.hour=(a.hour+1)%24;
return t;
}
ostream& operator<<(ostream& o,const Time& t)
{o<<setfill('0')<<setw(2)<<t.hour<<":"<<setw(2)<<t.minute<<":"
return o<<setw(2)<<t.second<<"\n"<<setfill(' ');
}
int main()
{Time t;
t.set(11,59,58);
cout<<t++;
cout<<++t;
}
搜索更多相关主题的帖子: void include public friend 
2007-11-14 12:49
踏魔狼
Rank: 6Rank: 6
等 级:贵宾
威 望:24
帖 子:1322
专家分:33
注 册:2005-9-22
收藏
得分:0 

#include<iomanip>
#include<iostream.h>

class Time{
private:
int hour,minute,second;
public:
Time()
{
hour = 0;
minute = 0;
second = 0;
}
Time(const Time & t)
{
hour = t.hour;
minute = t.minute;
second = t.second;
}
void set(int h,int m,int s)
{hour=h;minute=m;second=s;}
friend Time&operator++(Time& a);
friend Time operator++(Time& a,int);
friend ostream& operator<<(ostream& o,const Time& t);
};
Time& operator++(Time& a)
{
if(!(a.second=(a.second+1)%60)&&!(a.minute=(a.minute+1)%60))
a.hour=(a.hour+1)%24;
return a;
}
Time operator++(Time& a,int )
{Time t(a);
if(!(a.second=(a.second+1)%60)&&!(a.minute=(a.minute+1)%60))
a.hour=(a.hour+1)%24;
return t;
}
ostream& operator<<(ostream& o,const Time& t)
{
o<<t.hour<<":"<<t.minute<<":"<<t.second<<"\n";
return o;
}

int main()
{Time t;
t.set(11,59,58);
cout<<t++;
cout<<++t;
return 0;
}


=×&D o I p R e E n C g T l X&×=
2007-11-15 11:11
zjl138
Rank: 1
等 级:新手上路
威 望:1
帖 子:788
专家分:0
注 册:2007-11-12
收藏
得分:0 

太谢谢一楼了.麻烦你了.
不过我还是有点不明白使用友元时用#include<iostream>与#include<iostream.h>有什么不同.
是不是编译器问题.


i like linux...
2007-11-15 11:52
踏魔狼
Rank: 6Rank: 6
等 级:贵宾
威 望:24
帖 子:1322
专家分:33
注 册:2005-9-22
收藏
得分:0 

两个文件里面的东西可是不一样的,虽然功能是差不多.


=×&D o I p R e E n C g T l X&×=
2007-11-15 11:54
zjl138
Rank: 1
等 级:新手上路
威 望:1
帖 子:788
专家分:0
注 册:2007-11-12
收藏
得分:0 
那是不是用友元时就要用#include<iostream.h>头文件.
至少在VC++6.0的环境中是不是要这样.

i like linux...
2007-11-15 12:05
踏魔狼
Rank: 6Rank: 6
等 级:贵宾
威 望:24
帖 子:1322
专家分:33
注 册:2005-9-22
收藏
得分:0 
o<<setfill('0')<<setw(2)<<t.hour<<":"<<setw(2)<<t.minute<<":"
return o<<setw(2)<<t.second<<"\n"<<setfill(' ');

用了iostream后o<<t.hour这就是另一个函数调用Time类的私有变量,
这时就报错.
用那个头文件你自己看着办吧!

=×&D o I p R e E n C g T l X&×=
2007-11-15 12:17
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 
回复:(zjl138)太谢谢一楼了.麻烦你了.不过我还是有...

两个不同的标准,
建议使用#include<iostream>


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-11-15 13:11
zjl138
Rank: 1
等 级:新手上路
威 望:1
帖 子:788
专家分:0
注 册:2007-11-12
收藏
得分:0 
谢谢了

i like linux...
2007-11-15 13:11
zjl138
Rank: 1
等 级:新手上路
威 望:1
帖 子:788
专家分:0
注 册:2007-11-12
收藏
得分:0 

要不麻烦七楼"yuyunliuhen"用#include<iostream>此头文件将上面程序调试出来发给我.不胜感激!!!


i like linux...
2007-11-15 14:54
duccdd
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-10-25
收藏
得分:0 

除了
{o<<setfill('0')<<setw(2)<<t.hour<<":"<<setw(2)<<t.minute<<":"
少个分号外一切正常(vs2005)

2007-11-15 15:56
快速回复:此程序在错在哪里
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025106 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved