class Time{
//public:
int hour, minute, second;
public:
void set(int h, int m, int s){ hour=h, minute=m, second=s; }
friend Time& operator++(Time& a);
friend Time operator++(Time& a, int);
friend ostream& operator<<(ostream& o, const Time& t);
};
使用自定义类时,编译提示错误:cannot access private member declared in class 'Time'
开始以为类定义时默认是私有变量,加上public,似乎可以解决问题
其实还是没有声明类引起的。
class Time{
public:
int hour, minute, second;
public:
void set(int h, int m, int s){ hour=h, minute=m, second=s; }
friend Time& operator++(Time& a);
friend Time operator++(Time& a, int);
friend ostream& operator<<(ostream& o, const Time& t);
};
自定义类中重载操作符后,调用时,编译提示错误:'operator <<' is ambiguous
解决方法:
VC6.0 需要先声明类和重载操作符
class Time;
ostream& operator<<(ostream& o, const Time& t);
VC 6.0 在SP3之前, 对iostream与friend有Bug, 需要patch----SP5。可能这样的错误与编译器有关吧。