ostream重载问题
- C/C++ code
#include<iostream>#include <stdlib.h>using namespace std;class three_d{private: int x,y,z;public: three_d(int a,int b,int c):x(a),y(b),z(c) {} friend ostream & operator <<(ostream &s,three_d &obj);};ostream & operator <<(ostream &s,three_d &obj){ s<<obj.x<<","; s<<obj.y<<","; s<<obj.z<<","; return s;}int main(){ three_d a(1,2,3),b(3,4,5),c(6,7,8); cout<<a; system("pause"); return 0;}VC6编译报错
x,y和z cannot access private member declared in class 'three_d'
但是写成这样
- C/C++ code
#include<iostream>#include <stdlib.h>using namespace std;class three_d{private: int x,y,z;public: three_d(int a,int b,int c):x(a),y(b),z(c) {} friend ostream & operator <<(ostream &s,three_d &obj) { s<<obj.x<<","; s<<obj.y<<","; s<<obj.z<<","; return s; }};int main(){ three_d a(1,2,3),b(3,4,5),c(6,7,8); cout<<a; system("pause"); return 0;}确可以正确编译和链接....
请各位高手解释一下
[解决办法]
在VC6下运行第一个程序需要改头文件。
#include<iostream>改成#include<iostream.h>
并删去using namespace std;
在main中删去system("pause");
因为VC6没有完全实现C++标准。
[解决办法]