| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 831 人关注过本帖
标题:请教一个关于输入输出操作符重载问题
只看楼主 加入收藏
tenglvjun
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2007-5-10
收藏
 问题点数:0 回复次数:1 
请教一个关于输入输出操作符重载问题

请大家看这个程序:
#include<iostream>
using namespace std;

class X
{
public:

friend istream& operator>>(istream &in, X &m)
{
cout << "请输入数据:" << endl;

in >> m.c;

return in;
}

friend ostream& operator<<(ostream &out, X &m)
{
out << m.c << endl;

return out;
}

private:

char c;

};

int main()
{
X x;

cin >> x;

cout << x;

return 0;
}
这里我把友元类的输入输出的操作符重载声明跟定义在类的内部,当我在类中声明这个友元,在类外定义的话,就会出现
D:\程序\运算符重载\sadfasf.cpp(191) : error C2248: 'c' : cannot access private member declared in class 'X'
D:\程序\运算符重载\sadfasf.cpp(183) : see declaration of 'c'
D:\程序\运算符重载\sadfasf.cpp(199) : error C2248: 'c' : cannot access private member declared in class 'X'
D:\程序\运算符重载\sadfasf.cpp(183) : see declaration of 'c'
D:\程序\运算符重载\sadfasf.cpp(209) : error C2593: 'operator >>' is ambiguous
D:\程序\运算符重载\sadfasf.cpp(211) : error C2593: 'operator <<' is ambiguous
Error executing cl.exe.
这个编译的错误,想请教下大家这个是怎么回事?我记得友元是可以在类中声明在类外定义的啊~~
对了忘记说了我用的是VC++6.0

搜索更多相关主题的帖子: 操作符 operator 重载 输出 
2007-07-15 19:52
Arcticanimal
Rank: 3Rank: 3
等 级:论坛游民
威 望:7
帖 子:341
专家分:20
注 册:2007-3-17
收藏
得分:0 
换一种格式:
#include<iostream.h>
不用using namespce std;
这样就可以了

try new catch
2007-07-16 13:11
快速回复:请教一个关于输入输出操作符重载问题
数据加载中...
 
   



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

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