友元函数问题

goodmrning 2007-12-21 09:01:46
友元函数为什么函数体放在类的外面,不能调用类的私有成员?
...全文
92 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
grellen 2007-12-24
  • 打赏
  • 举报
回复
把#include <iostream>改成 #include <iostream.h>以后
那个string里的一些函数又不能用了,所以你还是把友员的定义放在里面就好了
grellen 2007-12-21
  • 打赏
  • 举报
回复
我用的是vc6.0
grellen 2007-12-21
  • 打赏
  • 举报
回复
难道我的编译器版本也太低,我编译上面的也会出现问题:


Compiling...
试.cpp
C:\Documents and Settings\green\桌面\新建文件夹 (2)\试.cpp(29) : error C2248: 'str' : cannot access private member declared in class 'longint'
C:\Documents and Settings\green\桌面\新建文件夹 (2)\试.cpp(7) : see declaration of 'str'
C:\Documents and Settings\green\桌面\新建文件夹 (2)\试.cpp(30) : error C2248: 'str' : cannot access private member declared in class 'longint'
C:\Documents and Settings\green\桌面\新建文件夹 (2)\试.cpp(7) : see declaration of 'str'
C:\Documents and Settings\green\桌面\新建文件夹 (2)\试.cpp(68) : error C2593: 'operator >>' is ambiguous
Error executing cl.exe.

试.exe - 3 error(s), 0 warning(s).
calss_cyl 2007-12-21
  • 打赏
  • 举报
回复


上面编译不是没有错误吗?
即:>>操作符 的定义放在类外面也是可以的啊。

你想问什么问题呢?
calss_cyl 2007-12-21
  • 打赏
  • 举报
回复

to:友元函数为什么函数体放在类的外面,不能调用类的私有成员?
reply:友元函数是对类来说,类让他访问自己的私有成员,但是就像是朋友一样,你让别人用自己的私有东西,但是别人还是个单独的个体撒,比如函数也可能是其他类的友元函数呢?你要是把函数体放自己类里面,别的类怎么让这个函数成为别人的友元呢?
友元函数当然可以调用类的私有成员了,这就是他存在的价值撒。

goodmrning 2007-12-21
  • 打赏
  • 举报
回复
typedef int Elemtype;
#include<iostream>
#include<string>
using namespace std;
class longint
{
string str;
Elemtype* list;
int Maxsize;

public:
longint(int ms=5);
longint(const longint& rhs);

void Insert_item(const string& str);

friend ostream& operator<<(ostream& os,const longint &exm)//为什么这调用可以?
{
for(int i=0;i<exm.str.length();i++)
os<<exm.list[i];
return os;

}


friend istream& operator>>(istream& in, longint& exm);//而这样又不行?
};
istream& operator>>(istream& in, longint& exm)
{
in>>exm.str;
exm.Insert_item(exm.str);
return in;
}
//===============================================
longint::longint(int ms)
{
if(ms<0)
ms=5;
Maxsize=ms;
list=new Elemtype[ms];
}

//================================================
longint::longint(const longint& rhs)
{

}
//==================================================
void longint::Insert_item(const string& str)
{ int i;
for(i=0;i<str.length();i++)
{
list[i]=str[i]-48;
if (str.length()==Maxsize)
{
Elemtype* p;
p=(Elemtype*)realloc(list,2*Maxsize*sizeof(Elemtype));
list=p;
Maxsize=2*Maxsize;
}
}
}
//=================================================


void main()
{
longint num1,num2;
cin>>num1;
cout<<num1;
}
飞哥 2007-12-21
  • 打赏
  • 举报
回复
不是吧,我的就能,难道是你vc6没打sp6补丁的原因?
mark21 2007-12-21
  • 打赏
  • 举报
回复
1楼正解, vc6编译会报错,vs2003 以上就不会
goodmrning 2007-12-21
  • 打赏
  • 举报
回复
是用了类对象啊
飞哥 2007-12-21
  • 打赏
  • 举报
回复
说错了,是类对象

飞哥 2007-12-21
  • 打赏
  • 举报
回复
需要通过类成员来访问
sinosinux 2007-12-21
  • 打赏
  • 举报
回复
那是你的编译器版本太低, 对标准C++支持的不好, 换vc2003以上或者gcc
飞哥 2007-12-21
  • 打赏
  • 举报
回复
要么你就装vs sp6
飞哥 2007-12-21
  • 打赏
  • 举报
回复
你将#include <iostream>
改成#include <iostream.h>即可

64,682

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧