标题:关于友元解决输入输出,大家帮忙看下,程序哪里出错了
取消只看楼主
心忆清凉
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-8-19
结帖率:0
已结贴  问题点数:20 回复次数:4 
关于友元解决输入输出,大家帮忙看下,程序哪里出错了
#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)
搜索更多相关主题的帖子: include public friend return Users 
2013-08-19 14:29
心忆清凉
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-8-19
得分:0 
回复 2楼 wp231957
我用的是vc.6++,编译就出现上面的错误提示
2013-08-19 14:45
心忆清凉
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-8-19
得分:0 
回复 3楼 rjsp
    friend void operator>>(std::istream& in,F& f);
    friend void operator<<(std::ostream& out,const F& f);
可以放在public下吗?
2013-08-19 14:49
心忆清凉
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-8-19
得分:0 
回复 3楼 rjsp
可以问一下,刚才我的那个程序到底是怎么出错了?
2013-08-19 15:01
心忆清凉
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-8-19
得分:0 
回复 7楼 rjsp
恩,好的,谢谢了
2013-08-19 19:00



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-419361-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.056184 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved