操作符重载的疑问:"<<"
随便写一个模板类:
#include <iostream>
template < class T >
class ZTest
{
friend std::ostream& operator << ( std::ostream& os,const ZTest<T>& test);
public:
ZTest ():a=0,b=0 {}
void set (T i,T j) { a=i;b=j; };
private:
T a;
T b;
};
template < class T>
std::ostream& << ( std::ostream& os, const ZTest<T>& test )
{
os <<"("<< test.a << " " << test.b << ")";
return os;
}
int main ()
{
ZTest<int> ti;
ti.set(9,10);
std::cout << ti << std::endl;
return 0;
}
编译后说operator << 中不能访问private 成员,a,b.可我已将它设为友函数了。怎么回事呢?求各位教教我.....