问一个关于操作符重载的问题
class T
{
public:
T():i_(0),j_(0) { };
friend ostream& operator < <(ostream& out,const T& t);
private:
int i_;
int j_;
};
ostream& operator < <(ostream& out,const T& t)
{
out < <t.i_;
out < <t.j_;
return out;
}
在运行时,会出现不能获取私有成员的错误,为什么呢,友员不是可以访问任何成员吗?
编译器是vc6.0.
[解决办法]
又是VC6,又是operator < <
friend ostream& operator < <(ostream& out,const T& t)
{
out < <t.i_;
out < <t.j_;
return out;
}
用“友元就地实现 "。
[解决办法]
呵呵 VC6早该扔垃圾堆了
[解决办法]
楼主不必自责, 是编译器的问题.
这个问题困扰了我半个月, 我最终最好的办法是, 做一个前置声明:
class T
ostream& operator < <(ostream& out,const T& t);
class T
{
public:
T():i_(0),j_(0) { };
friend ostream& operator < <(ostream& out,const T& t);
.....
}