| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 520 人关注过本帖
标题:关于<<重载问题,望指教
只看楼主 加入收藏
mfkpgfsbihc
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-11-25
收藏
 问题点数:0 回复次数:4 
关于<<重载问题,望指教
在ry.h中有如下声明:
#ifndef TIME_H
#define TIME_H
class Time{
private:
      int hours;
      int minuets;
public:
     Time();
     Time(int h,int m=0);
     void addMin(int m);
     void addHr(int h);
     void reset(int h=0,int m=0);
     Time operator+(const Time& t)const;
     Time operator-(const Time& t)const;
     Time operator*(double n)const;
     friend ostream& operator<<(ostream &os,const Time &t);
    // void show()const;
};
#endif
其定义在ry.cpp中,如下:
#include"ry.h"
//#include<iostream>
Time::Time(){
    hours=0;
    minuets=0;
}
Time::Time(int h,int m){
    hours=h;
    minuets=m;
}
void Time::addMin(int m){
     minuets+=m;
    hours+=minuets/60;
    minuets%=60;
}
void Time::addHr(int h){
    hours+=h;
}
void Time::reset(int h,int m){
     hours=h;
     minuets=m;
}
Time Time::operator+(const Time &t)const{
     Time sum;
     sum.minuets=minuets+t.minuets;
     sum.hours=hours+t.hours+sum.minuets/60;
     sum.minuets%=60;
     return sum;
}
Time Time::operator-(const Time &t)const{
      Time diff;
      int total1=minuets+60*hours;
      int total2=t.minuets+60*t.hours;
      diff.minuets=(total1-total2)%60;
      diff.hours=(total1-total2)/60;
      return diff;
}
Time Time::operator*(double n)const{
       Time t;
       t.minuets=(int)(minuets*n);
       t.hours=(int)(hours*n)+t.minuets/60;
       t.minuets%=60;
       return t;
}
ostream & operator<<(ostream &os,const Time &t){
     os<<"hours:"<<hours<<"\n"<<"minuets:"<<minuets<<endl;
    return os;
}
/*
void Time::show()const{
 std::cout<<"hours:"<<hours<<std::endl;
 std::cout<<"minuets:"<<minuets<<std::endl;
}*/
请大家给我看看为什么不能识别ostream流
搜索更多相关主题的帖子: 重载 指教 
2007-12-01 15:27
中学者
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:20
帖 子:3554
专家分:80
注 册:2007-9-14
收藏
得分:0 
没懂你问什么~~~

樱花大战,  有爱.
2007-12-01 16:01
mfkpgfsbihc
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-11-25
收藏
得分:0 
麻烦你们把代码在你们机子上运行一下就行了,这个我实在不知怎么措词呢
2007-12-01 16:13
中学者
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:20
帖 子:3554
专家分:80
注 册:2007-9-14
收藏
得分:0 
你把运行错误贴来看下~

樱花大战,  有爱.
2007-12-01 16:25
mfkpgfsbihc
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-11-25
收藏
得分:0 
error C2143: syntax error : missing ';' before '&'
F:\程序设计\ry.cpp(52) : error C2501: 'ostream' : missing storage-class or type specifiers
F:\程序设计\ry.cpp(52) : error C2061: syntax error : identifier 'ostream'
F:\程序设计\ry.cpp(52) : error C2501: '<<' : missing storage-class or type specifiers
F:\程序设计\ry.cpp(52) : error C2809: 'operator <<' has no formal parameters
F:\程序设计\ry.cpp(53) : error C2065: 'os' : undeclared identifier
F:\程序设计\ry.cpp(53) : error C2297: '<<' : illegal, right operand has type 'char [7]'
F:\程序设计\ry.cpp(53) : error C2065: 'hours' : undeclared identifier
F:\程序设计\ry.cpp(53) : error C2065: 'minuets' : undeclared identifier
Error executing cl.exe.
2007-12-01 17:06
快速回复:关于<<重载问题,望指教
数据加载中...
 
   



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

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