BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #29923同步于 2009/10/14
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

[求助]友元函数无法调用私有变量,问题出在哪里?

zmw
2009/10/14镜像同步16 回复
在一个类中,我已经定义了友元函数,可在编译时确出错,说该函数无法使用该类的私有变量,这是怎么回事?
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
LOVEBABY机器人#1 · 2009/10/14
【 在 zmw 的大作中提到: 】 : 在一个类中,我已经定义了友元函数,可在编译时确出错,说该函数无法使用该类的私有变量,这是怎么回事? 代码拿出来,真相马上就有了
zmw机器人#2 · 2009/10/14
【 在 LOVEBABY 的大作中提到: 】 : 代码拿出来,真相马上就有了 #include <iostream> using namespace std; class DigitalTime { public: friend bool operator == (const DigitalTime& time1, const DigitalTime& time2); //bool operator == (const DigitalTime& time1); DigitalTime(int the_hour, int the_minute); DigitalTime(); void advance(int minutes_added); void advance(int hours_added, int minutes_added); friend istream& operator >> (istream& ins, DigitalTime& the_object); friend ostream& operator << (ostream& outs, const DigitalTime& the_object); private: int hour; int minute; }; #include <iostream> #include <cctype> #include <cstdlib> #include "dtime.h" using namespace std; void read_hour(istream& ins, int& the_hour); void read_minute(istream& ins, int& the_mintue); int digit_to_int(char c); bool operator ==(const DigitalTime& time1, const DigitalTime& time2) { return (time1.hour == time2.hour && time1.minute == time2.minute); } DigitalTime::DigitalTime(int the_hour, int the_minute) { if (the_hour < 0 || the_hour > 23 || the_minute < 0 || the_minute > 59) { cout << "Illegal input."; exit(1); } else { hour = the_hour; minute = the_minute; } } DigitalTime::DigitalTime() : hour(0), minute(0) { } void DigitalTime::advance(int minutes_added) { int gross_minutes = minute + minutes_added; minute = gross_minutes%60; int hour_adjustment = gross_minutes/60; hour = (hour + hour_adjustment)%24; } void DigitalTime::advance(int hours_added, int minutes_added) { hour = (hour + hours_added)%24; advance(minutes_added); } ostream& operator << (ostream& outs, const DigitalTime& the_object) { outs << the_object.hour << ':'; if (the_object.minute < 10) outs << '0'; outs << the_object.minute; return outs; } istream& operator >> (istream& ins, DigitalTime& the_object) { read_hour(ins, the_object.hour); read_minute(ins, the_object.minute); return ins; } int digit_to_int(char c) { return ( int(c) - int('0') ); } void read_minute(istream& ins, int& the_minute) { char c1, c2; ins >> c1 >> c2; if (!(isdigit(c1) && isdigit(c2))) { cout << "Error illegal input\n"; exit(1); } the_minute = digit_to_int(c1)*10 + digit_to_int(c2); if (the_minute < 0 || the_minute > 59) { cout << "Error illegal input\n"; exit(1); } } void read_hour(istream& ins, int& the_hour) { char c1, c2; ins >> c1 >> c2; if (!(isdigit(c1) && (isdigit(c2) || c2 == ':') ) ) { cout << "Error illegal input\n"; exit(1); } if (isdigit(c1) && c2 == ':') { the_hour = digit_to_int(c1); } else { the_hour = digit_to_int(c1)*10 + digit_to_int(c2); ins >> c2; if (c2 != ':') { cout << "Error illegal input\n"; exit(1); } } if ( the_hour < 0 || the_hour > 23 ) { cout << "Error illegal input\n"; exit(1); } }
zmw机器人#3 · 2009/10/14
【 在 LOVEBABY 的大作中提到: 】 : 代码拿出来,真相马上就有了 呵呵,真相如上
Vampire机器人#4 · 2009/10/14
貌似前几天有人问过同样的问题
ding328机器人#5 · 2009/10/14
【 在 zmw 的大作中提到: 】 : #include <iostream> : using namespace std; : class DigitalTime : ................... 我能编译通过啊
AFX机器人#6 · 2009/10/14
【 在 zmw 的大作中提到: 】 : 呵呵,真相如上 真相就是:请彻底抛弃VC6.0.....
vcpp机器人#7 · 2009/10/14
我用g++编译也能通过啊,运行测试也没有问题。 【 在 ding328 的大作中提到: 】 : 我能编译通过啊
Vampire机器人#8 · 2009/10/14
实际上前几天那个帖子讨论的结果就是换个编译器搞起……
jokerlee机器人#9 · 2009/10/14
V6的bug 需要前向声明一下