关于友元解决输入输出,大家帮忙看下,程序哪里出错了
#include<iostream>#include<string>
using namespace std;
class F{
int n;
int d;
friend void operator>>(istream& in,F& f);
friend void operator<<(ostream& out,const F& f);
public:
F(int n=0,int d=1):n(n),d(d){};
~F(){};
};
istream& operator>>(istream& in,F& f){
char c;
in>>f.n>>c>>f.d>>endl;
}
ostream& operator<<(ostream& out,const F& f){
out<<f.n<<'/'<<f.d<<endl;
}
int main ()
{
F a;
cin>>a;
cout<<a;
return 0;
}
错误提示:C:\Users\kjk\c编程\输入与输出.cpp(17) : error C2248: 'n' : cannot access private member declared in class 'F'
C:\Users\kjk\c编程\输入与输出.cpp(6) : see declaration of 'n'
C:\Users\kjk\c编程\输入与输出.cpp(17) : error C2248: 'd' : cannot access private member declared in class 'F'
C:\Users\kjk\c编程\输入与输出.cpp(7) : see declaration of 'd'
C:\Users\kjk\c编程\输入与输出.cpp(17) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type '' (or there is no acceptable conversion)
C:\Users\kjk\c编程\输入与输出.cpp(20) : error C2248: 'n' : cannot access private member declared in class 'F'
C:\Users\kjk\c编程\输入与输出.cpp(6) : see declaration of 'n'
C:\Users\kjk\c编程\输入与输出.cpp(20) : error C2248: 'd' : cannot access private member declared in class 'F'
C:\Users\kjk\c编程\输入与输出.cpp(7) : see declaration of 'd'
C:\Users\kjk\c编程\输入与输出.cpp(25) : error C2593: 'operator >>' is ambiguous
C:\Users\kjk\c编程\输入与输出.cpp(26) : error C2593: 'operator <<' is ambiguous
执行 cl.exe 时出错.
输入与输出.exe - 1 error(s), 0 warning(s)