求解:c++程序的友元函数重载输出流运算符VC6.0编译错误

头文件:#ifndef_MYVECTOR_H_#define_MYVECTOR_H_#include<iostream>#include<string>classmyve... 头文件:
#ifndef _MYVECTOR_H_
#define _MYVECTOR_H_
#include<iostream>
#include<string>
class myvector
{
private:
int x;
int y;
int z;
public:
myvector();
myvector(int x,int y,int z);
myvector operator+(myvector v);
friend ostream& operator<<(ostream& out,myvector b);
};
#endif
下面是源程序:
#include<iostream>
#include"vector.h"
using namespace std;
myvector::myvector()
{
x=1;
y=1;
z=1;
}
myvector::myvector(int X,int Y,int Z)
{
x=X;
y=Y;
z=Z;
}
myvector myvector::operator +(myvector v)
{
myvector temp;
temp.x=this->x+v.x;
temp.y=this->y+v.y;
temp.z=this->z+v.z;
return temp;
}
ostream& operator<<(ostream& out,myvector b)
{
out<<"("<<b.x<<","<<b.y<<","<<b.z<<")"<<endl;
return out;
}
void main()
{
myvector v1(7,8,9);
myvector v2;
myvector v3;
v3=v1.operator + (v2);
cout<<v3;

}
下面是error:
c:\program files (x86)\microsoft visual studio\myprojects\my\vector.h(15) : error C2143: syntax error : missing ';' before '&'
c:\program files (x86)\microsoft visual studio\myprojects\my\vector.h(15) : error C2433: 'ostream' : 'friend' not permitted on data declarations
c:\program files (x86)\microsoft visual studio\myprojects\my\vector.h(15) : error C2501: 'ostream' : missing storage-class or type specifiers
c:\program files (x86)\microsoft visual studio\myprojects\my\vector.h(15) : error C2244: 'ostream' : unable to resolve function overload
c:\program files (x86)\microsoft visual studio\myprojects\my\vector.h(15) : error C2061: syntax error : identifier 'ostream'
c:\program files (x86)\microsoft visual studio\myprojects\my\vector.h(15) : error C2501: '<<' : missing storage-class or type specifiers
c:\program files (x86)\microsoft visual studio\myprojects\my\vector.h(15) : error C2805: binary 'operator <<' has too few parameters
c:\program files (x86)\microsoft visual studio\myprojects\my\main.cpp(27) : error C2248: 'x' : cannot access private member declared in class 'myvector'
c:\program files (x86)\microsoft visual studio\myprojects\my\vector.h(8) : see declaration of 'x'
c:\program files (x86)\microsoft visual studio\myprojects\my\main.cpp(27) : error C2248: 'y' : cannot access private member declared in class 'myvector'
c:\program files (x86)\microsoft visual studio\myprojects\my\vector.h(9) : see declaration of 'y'
c:\program files (x86)\microsoft visual studio\myprojects\my\main.cpp(27) : error C2248: 'z' : cannot access private member declared in class 'myvector'
c:\program files (x86)\microsoft visual studio\myprojects\my\vector.h(10) : see declaration of 'z'
执行 cl.exe 时出错.

求解!!
展开
 我来答
龍__鳳
2014-01-24 · TA获得超过2346个赞
知道小有建树答主
回答量:948
采纳率:0%
帮助的人:1438万
展开全部
friend std::ostream& operator<<(std::ostream& out,myvector b);

重载函数这样声明,就行了

我和高楼寻缝想的一样,但是我先是加上了using namespace std;仍有错误提示,不能访问私有成员,但是不加using namespace std;而只在ostream前加上std::居然没错了……

追问
不好意思,我刚才少加了一个,,现在可以了,不过,还是想请问你为什么要这样改呢?这个重载运算符是书上照搬的。
追答
呵呵,我也没想明白,我觉得是命名空间的问题。C++primer上也是加上std的。全局命名空间也有类ostream,std命名空间也有。但我不知道为什么声明了命名空间std后,却提示不能访问私有成员,而且主函数里提示<<使用有歧义

还有另外一种改法:
在vector.h里加上using namespace std;
在cpp文件里的重载函数定义这样写:
ostream& std::operator<<(ostream& out,myvector b)
标明运算符<<是std命名空间下的。

这样可以解释只在vector.h加上using namespace std;后<<使用有歧义,且不能访问私有成员的问题了,这是因为,你在cpp里重载的<<不是你在vector.h里定义为友元的<<函数,虽然他们名字一样,但不是定义在同一个命名空间下的函数。
你可以重新定义一个友元函数,函数名改成别的,这样加上using namespace std;后就不会有不能访问私有成员的错误了
以上是个人见解,请各位指正!
水焱新火
2014-01-24 · TA获得超过256个赞
知道小有建树答主
回答量:144
采纳率:100%
帮助的人:132万
展开全部
奇怪了,我复制了你的代码在一个cpp可以正常运行,仔细一看,头文件里面已经使用了ostream,但是头文件却没有申明使用了std,他就找不到了
追问
你好,谢谢你的回答。我在头文件里加上申明使用std后,还是会提示无法访问私有成员,但是友元函数不是可以访问么?
追答
具体给他图看看,你的代码我复制运行过,没有问题啊
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
剧镶天0fR425
2014-01-24
知道答主
回答量:18
采纳率:0%
帮助的人:8.5万
展开全部
哇给我哈狗娃福娃
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
高楼寻缝
2014-01-24 · TA获得超过175个赞
知道小有建树答主
回答量:160
采纳率:0%
帮助的人:164万
展开全部
在头文件中加上下面这句试试。

using namespace std;
追问
你好,谢谢你的回答。加上这句后会error提示无法访问私有成员。但既然是友元函数为什么不能访问呢?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 3条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式