注册 登录
编程论坛 C++教室

重载操作符中碰到的问题,不能读取类的私有数据成员

heyyroup 发布于 2007-09-01 12:17, 1008 次点击
今天看到重载操作符。但是不是很理解。谁能帮忙解答一下:
以下是程序
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;
}
......
编译的时候说不能读取类的私有数据成员。那么应该如何改正?重载操作符时应该注意些什么呢??
4 回复
#2
雨中飞燕2007-09-01 12:53
用友元函数



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]
#3
天下第二刀2007-09-01 13:13


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


#4
heyyroup2007-09-01 14:15

编译出现这些错误
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)
该怎么办呢?

#5
heyyroup2007-09-01 14:17
哦,难怪出现这么多错误
明白是哪里错呢,谢谢大家的帮助
1