随笔 - 2, 文章 - 0, 评论 - 3, 引用 - 0
数据加载中……

2009年1月15日

cannot access private member

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,似乎可以解决问题
其实还是没有声明类引起的。

posted @ 2009-01-15 15:36 Robbin 阅读(1292) | 评论 (0)编辑 收藏

'operator <<' is ambiguous

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。可能这样的错误与编译器有关吧。

posted @ 2009-01-15 15:23 Robbin 阅读(3280) | 评论 (3)编辑 收藏