| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1018 人关注过本帖
标题:关于友元解决输入输出,大家帮忙看下,程序哪里出错了
只看楼主 加入收藏
心忆清凉
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-8-19
结帖率:0
收藏
已结贴  问题点数:20 回复次数:7 
关于友元解决输入输出,大家帮忙看下,程序哪里出错了
#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
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:10 
这是汉化版vs2010的编译信息


1>  正在创建“Debug\t7.unsuccessfulbuild”,因为已指定“AlwaysCreate”。
1>ClCompile:
1>  stdafx.cpp
1>  t7.cpp
1>d:\c_source\t7\t7\t7.cpp(15): error C2556: “std::istream &operator >>(std::istream &,F &)”: 重载函数与“void operator >>(std::istream &,F &)”只是在返回类型上不同
1>          d:\c_source\t7\t7\t7.cpp(8) : 参见“operator >>”的声明
1>d:\c_source\t7\t7\t7.cpp(15): error C2040: “operator >>”:“std::istream &(std::istream &,F &)”与“void (std::istream &,F &)”的间接寻址级别不同
1>d:\c_source\t7\t7\t7.cpp(17): error C2678: 二进制“>>”: 没有找到接受“std::basic_istream<_Elem,_Traits>”类型的左操作数的运算符(或没有可接受的转换)

1>d:\c_source\t7\t7\t7.cpp(19): error C2556: “std::ostream &operator <<(std::ostream &,const F &)”: 重载函数与“void operator <<(std::ostream &,const F &)”只是在返回类型上不同
1>          d:\c_source\t7\t7\t7.cpp(9) : 参见“operator <<”的声明
1>d:\c_source\t7\t7\t7.cpp(19): error C2040: “operator <<”:“std::ostream &(std::ostream &,const F &)”与“void (std::ostream &,const F &)”的间接寻址级别不同
1>d:\c_source\t7\t7\t7.cpp(25): error C2264: “operator >>”: 函数定义或声明中有错误;未调用函数
1>d:\c_source\t7\t7\t7.cpp(25): error C2088: “>>”: 对于 class 非法
1>d:\c_source\t7\t7\t7.cpp(26): error C2264: “operator <<”: 函数定义或声明中有错误;未调用函数
1>d:\c_source\t7\t7\t7.cpp(26): error C2088: “<<”: 对于 class 非法
1>
1>生成失败。
1>
1>已用时间 00:00:02.93
========== 全部重新生成: 成功 0 个,失败 1 个,跳过 0 个 ==========

DO IT YOURSELF !
2013-08-19 14:38
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:10 
#include <iostream>

class F {
public:
    F(int n=0, int d=1) : n_(n), d_(d) {
    };

private:
    int n_;
    int d_;

    friend std::istream& operator>>(std::istream& in, F& f);
    friend std::ostream& operator<<(std::ostream& out, const F& f);
};
std::istream& operator>>(std::istream& in, F& f) {
    char c;
    return in>>f.n_>>c>>f.d_;
}
std::ostream& operator<<(std::ostream& out, const F& f) {
    return out<<f.n_<<'/'<<f.d_;
}

using namespace std;

int main ()
{
    F a;
    cin >> a;
    cout << a << endl;

    return 0;
}
2013-08-19 14:39
心忆清凉
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
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
回复 5楼 心忆清凉
可以,放在任何地方都没有关系。你自己想想是不是这样,学东西要勤于思考。

你的错误很多,最主要的是
friend的是 void operator>>(istream& in,F& f)
定义的是 istream& operator>>(istream& in,F& f)
不是同一个函数呀
2013-08-19 15:11
心忆清凉
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-8-19
收藏
得分:0 
回复 7楼 rjsp
恩,好的,谢谢了
2013-08-19 19:00
快速回复:关于友元解决输入输出,大家帮忙看下,程序哪里出错了
数据加载中...
 
   



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

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