| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1006 人关注过本帖
标题:重载操作符中碰到的问题,不能读取类的私有数据成员
只看楼主 加入收藏
heyyroup
Rank: 1
等 级:新手上路
帖 子:77
专家分:0
注 册:2006-6-14
收藏
 问题点数:0 回复次数:4 
重载操作符中碰到的问题,不能读取类的私有数据成员
今天看到重载操作符。但是不是很理解。谁能帮忙解答一下:
以下是程序
class Time
{
private:
int hours;
int minutes;
public:
Time();
Time(int h,int m=0);
void AddMin(int m);
void AddHr(int h);
void Reset(int h=0,int m=0);
friend Time operator+(const Time & t1,const Time t2);
friend Time operator-(const Time & t1,const Time t2);
friend Time operator*(double n,const Time t);
void Show() const;
};
......
Time operator+(const Time & t1,const Time &t2)
{
Time sum;
sum.minutes=t1.minutes+t2.minutes;
sum.hours=t1.hours+t2.hours+sum.minutes/60;
sum.minutes%=60;
return sum;
}
Time operator-(const Time & t1,const Time &t2)
{
Time diff;
int tot1,tot2;
tot1=t1.minutes+60*t1.hours;
tot2=t2.minutes+60*t2.hours;
diff.minutes=(tot2-tot1)%60;
diff.hours=(tot2-tot1)/60;
return diff;
}
Time operator*(double n,const Time & t)
{
Time result;
long totalminutes = t.hours*n*60+minutes*n;
result.hours=totalminutes/60;
result.minutes=totalminutes%60;
return result;
}
......
编译的时候说不能读取类的私有数据成员。那么应该如何改正?重载操作符时应该注意些什么呢??
搜索更多相关主题的帖子: operator const Time int 操作符 
2007-09-01 12:17
雨中飞燕
Rank: 3Rank: 3
等 级:禁止访问
威 望:8
帖 子:2200
专家分:0
注 册:2007-8-9
收藏
得分:0 
用友元函数



by 雨中飞燕 QQ:78803110 QQ讨论群:5305909

[url=http://bbs.bc-cn.net/viewthread.php?tid=163571]请大家不要用TC来学习C语言,点击此处查看原因[/url]
C/C++算法习题(OnlineJudge):[url]http://yzfy.org/[/url]
2007-09-01 12:53
天下第二刀
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:157
专家分:0
注 册:2007-1-8
收藏
得分:0 


long totalminutes = t.hours*n*60 + minutes*n;  //  少了个 t , t.minutes*n

friend Time operator+(const Time & t1,const Time t2); // const Time & t2
friend Time operator-(const Time & t1,const Time t2); // const Time & t2
friend Time operator*(double n,const Timet); // const Time & t



不知天堂有没有后门~~~
2007-09-01 13:13
heyyroup
Rank: 1
等 级:新手上路
帖 子:77
专家分:0
注 册:2006-6-14
收藏
得分:0 

编译出现这些错误
Compiling...
mytime2.cpp
E:\C++程序\Lianxi11.4\mytime2.cpp(25) : error C2248: 'minutes' : cannot access private member declared in class 'Time'
e:\c++程序\lianxi11.4\mytime2.h(7) : see declaration of 'minutes'
E:\C++程序\Lianxi11.4\mytime2.cpp(25) : error C2248: 'minutes' : cannot access private member declared in class 'Time'
e:\c++程序\lianxi11.4\mytime2.h(7) : see declaration of 'minutes'
E:\C++程序\Lianxi11.4\mytime2.cpp(25) : error C2248: 'minutes' : cannot access private member declared in class 'Time'
e:\c++程序\lianxi11.4\mytime2.h(7) : see declaration of 'minutes'
E:\C++程序\Lianxi11.4\mytime2.cpp(26) : error C2248: 'hours' : cannot access private member declared in class 'Time'
e:\c++程序\lianxi11.4\mytime2.h(6) : see declaration of 'hours'
E:\C++程序\Lianxi11.4\mytime2.cpp(26) : error C2248: 'hours' : cannot access private member declared in class 'Time'
e:\c++程序\lianxi11.4\mytime2.h(6) : see declaration of 'hours'
E:\C++程序\Lianxi11.4\mytime2.cpp(26) : error C2248: 'hours' : cannot access private member declared in class 'Time'
e:\c++程序\lianxi11.4\mytime2.h(6) : see declaration of 'hours'
E:\C++程序\Lianxi11.4\mytime2.cpp(26) : error C2248: 'minutes' : cannot access private member declared in class 'Time'
e:\c++程序\lianxi11.4\mytime2.h(7) : see declaration of 'minutes'
E:\C++程序\Lianxi11.4\mytime2.cpp(27) : error C2248: 'minutes' : cannot access private member declared in class 'Time'
e:\c++程序\lianxi11.4\mytime2.h(7) : see declaration of 'minutes'
E:\C++程序\Lianxi11.4\mytime2.cpp(34) : error C2248: 'minutes' : cannot access private member declared in class 'Time'
e:\c++程序\lianxi11.4\mytime2.h(7) : see declaration of 'minutes'
E:\C++程序\Lianxi11.4\mytime2.cpp(34) : error C2248: 'hours' : cannot access private member declared in class 'Time'
e:\c++程序\lianxi11.4\mytime2.h(6) : see declaration of 'hours'
E:\C++程序\Lianxi11.4\mytime2.cpp(35) : error C2248: 'minutes' : cannot access private member declared in class 'Time'
e:\c++程序\lianxi11.4\mytime2.h(7) : see declaration of 'minutes'
E:\C++程序\Lianxi11.4\mytime2.cpp(35) : error C2248: 'hours' : cannot access private member declared in class 'Time'
e:\c++程序\lianxi11.4\mytime2.h(6) : see declaration of 'hours'
E:\C++程序\Lianxi11.4\mytime2.cpp(36) : error C2248: 'minutes' : cannot access private member declared in class 'Time'
e:\c++程序\lianxi11.4\mytime2.h(7) : see declaration of 'minutes'
E:\C++程序\Lianxi11.4\mytime2.cpp(37) : error C2248: 'hours' : cannot access private member declared in class 'Time'
e:\c++程序\lianxi11.4\mytime2.h(6) : see declaration of 'hours'
E:\C++程序\Lianxi11.4\mytime2.cpp(43) : error C2248: 'hours' : cannot access private member declared in class 'Time'
e:\c++程序\lianxi11.4\mytime2.h(6) : see declaration of 'hours'
E:\C++程序\Lianxi11.4\mytime2.cpp(43) : error C2248: 'minutes' : cannot access private member declared in class 'Time'
e:\c++程序\lianxi11.4\mytime2.h(7) : see declaration of 'minutes'
E:\C++程序\Lianxi11.4\mytime2.cpp(44) : error C2248: 'hours' : cannot access private member declared in class 'Time'
e:\c++程序\lianxi11.4\mytime2.h(6) : see declaration of 'hours'
E:\C++程序\Lianxi11.4\mytime2.cpp(45) : error C2248: 'minutes' : cannot access private member declared in class 'Time'
e:\c++程序\lianxi11.4\mytime2.h(7) : see declaration of 'minutes'
执行 cl.exe 时出错.

mytime2.obj - 1 error(s), 0 warning(s)
该怎么办呢?

2007-09-01 14:15
heyyroup
Rank: 1
等 级:新手上路
帖 子:77
专家分:0
注 册:2006-6-14
收藏
得分:0 
哦,难怪出现这么多错误
明白是哪里错呢,谢谢大家的帮助
2007-09-01 14:17
快速回复:重载操作符中碰到的问题,不能读取类的私有数据成员
数据加载中...
 
   



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

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