| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 649 人关注过本帖
标题:关于<<重载问题
只看楼主 加入收藏
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;
}*/
我实在不知道这个该怎么措词,望各位弄到你们机子上运行一下就明白了,我运行这几个错误就是不知道是什么原因造成的
yntax 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:09
中学者
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:20
帖 子:3554
专家分:80
注 册:2007-9-14
收藏
得分:0 
重载右移要声明为友元~

樱花大战,  有爱.
2007-12-01 17:14
mfkpgfsbihc
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-11-25
收藏
得分:0 
我已经声明为友无了啊.
friend ostream& operator<<(ostream &os,const Time &t);
2007-12-01 17:21
中学者
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:20
帖 子:3554
专家分:80
注 册:2007-9-14
收藏
得分:0 
ry.h里面没有包含<iostream>

樱花大战,  有爱.
2007-12-01 17:24
mfkpgfsbihc
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-11-25
收藏
得分:0 
谢,解决之
2007-12-01 17:37
快速回复:关于<<重载问题
数据加载中...
 
   



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

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