首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

ostream重载有关问题

2012-05-22 
ostream重载问题C/C++ code#includeiostream#include stdlib.husing namespace stdclass three_d{pri

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++标准。
[解决办法]
探讨

在VC6下运行第一个程序需要改头文件。
#include<iostream>改成#include<iostream.h>
并删去using namespace std;
在main中删去system("pause");
因为VC6没有完全实现C++标准。

热点排行