"<<"重载(声明为友元)错在哪里? ,在线。
#include <iostream>
#include <string>
using namespace std;
class U{
public:
U(int m,int n){
i=m; //(12)行
j=n; //(13)
}
protected:
int i;
int j;
friend ostream& operator << (ostream& oo,U u); //友元
};
ostream& operator << (ostream& oo,U u){
oo << u.i << u.j; //(21)
return oo;
}
void main()
{
U u(3,5);
cout << u; //(28)
}
(21) : error C2248: 'i' : cannot access protected member declared in class 'U'
(12) : see declaration of 'i'
(21) : error C2248: 'j' : cannot access protected member declared in class 'U'
(13) : see declaration of 'j'
(28)error C2593: 'operator <<' is ambiguous