关于操作符重载的问题
在重载时为什么用#include<iostream>头文件重载<<,>>,==操作符时 明明已经把重载函数申明位友元了,但是在重载函数定义时却不能调用私有成员?把头文件改成#include<iostream.h> 注释掉using namespace std;就可以了呢? 但是在用了#include<iostream.h>后string类又出问题了 请高手指点#include<iostream>
using namespace std;
class C
{
public:
C():x(0),y(1){}
friend ostream &operator << (ostream &,const C &);
private:
int x;
int y;
};
ostream &operator << (ostream &out,const C &c1)
{
out<<c1.x<<" "<<c1.y<<endl;
return out;
}
int main()
{
C cc;
cout<<cc;
return 0;
}
编译错误
:\program files\microsoft visual studio\myprojects\iostream\io.cpp(17) : error C2248: 'x' : cannot access private member declared in class 'C'
g:\program files\microsoft visual studio\myprojects\iostream\io.cpp(11) : see declaration of 'x'
g:\program files\microsoft visual studio\myprojects\iostream\io.cpp(17) : error C2248: 'y' : cannot access private member declared in class 'C'
g:\program files\microsoft visual studio\myprojects\iostream\io.cpp(12) : see declaration of 'y'
g:\program files\microsoft visual studio\myprojects\iostream\io.cpp(24) : error C2593: 'operator <<' is ambiguous
但是头文件改成#include<iostream.h>却可以呢