注册 登录
编程论坛 C++教室

请教一个关于输入输出操作符重载问题

tenglvjun 发布于 2007-07-15 19:52, 836 次点击

请大家看这个程序:
#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

1 回复
#2
Arcticanimal2007-07-16 13:11
换一种格式:
#include<iostream.h>
不用using namespce std;
这样就可以了
1