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