关于友员函数

xxne2002 2004-05-04 11:14:10
#include <iostream>
using namespace std;

class Score
{
friend ostream & operator<<(ostream & os, const Score & s);
private:
unsigned int num;
public:
Score():num(0){};

};


ostream & operator<<(ostream & os, const Score & s)
{
os<<s.num;
return os;
}

为什么在VC6.0下出现如下错误:
--------------------Configuration: test - Win32 Debug--------------------
Compiling...
score.cpp
C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\MYPROJECTS\test\score.cpp(17) : error C2248: 'num' : cannot access private member declared in class 'Score'
C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\MYPROJECTS\test\score.cpp(8) : see declaration of 'num'
Error executing cl.exe.

score.obj - 1 error(s), 0 warning(s)
...全文
44 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
梦想成了相扑 2004-08-26
  • 打赏
  • 举报
回复
这是vc6.0的bug,请看:http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q192/5/39.asp&NoWebContent=1

解决办法有两个:
1、下载sp6,地址:http://www.microsoft.com/downloads/details.aspx?FamilyId=A8494EDB-2E89-4676-A16A-5C5477CB9713&displaylang=en
2、不要使用 using namespace std 及类似的语句!这样在代码中使用std中的类时就得完全限定之了。如:把ostream改成std::ostream。

祝你好运!!
sharkhuang 2004-05-05
  • 打赏
  • 举报
回复
vc6.0太老了
cngdzhang 2004-05-04
  • 打赏
  • 举报
回复
我在做模板的时候也有类似的问题,可能是vc的问题把

解决问题,我是把声明和实现都放在了一起的,即都放在了类声明的里面
freefalcon 2004-05-04
  • 打赏
  • 举报
回复
这本来就是VC自己的问题,没有什么技术问题可以讨论
wzsy(无中生有)说的情况是可以的,因为VC6.0对于自定义函数声明成友元似乎还能较好的支持,但对于诸如operator<<这样的操作符就不行了
wzsy 2004-05-04
  • 打赏
  • 举报
回复
#include <iostream>

using namespace std;
class Point
{
friend void ChangePrivate( Point & );
public:
Point( void ) : m_i(0) {}
void PrintPrivate( void ){cout << m_i << endl; }

private:
int m_i;
};

void ChangePrivate ( Point &i ) { i.m_i++; }

int main()
{
Point sPoint;
sPoint.PrintPrivate();
ChangePrivate(sPoint);
sPoint.PrintPrivate();
}
为什么msdn中的这个例子就能运行
carambo 2004-05-04
  • 打赏
  • 举报
回复
最好就是不要用友元
newegg2002 2004-05-04
  • 打赏
  • 举报
回复
学习学习,,
我知道将友元函数的定义在类中实现可以解决这样的问题,,,
freefalcon 2004-05-04
  • 打赏
  • 举报
回复
vc6.0对标准C++支持不好的缘故
临时解决方法为
1. 前项声明
class Score;
ostream & operator<<(ostream & os, const Score & s);
class Score
{
friend ostream & operator<<(ostream & os, const Score & s)
{
os<<s.num;
return os;
}
private:
unsigned int num;
public:
Score():num(0){};

};

2. 将友元的定义放到类体里
ostream & operator<<(ostream & os, const Score & s);

3. 打VC的SP补丁
julyclyde 2004-05-04
  • 打赏
  • 举报
回复
哦?那就是Microsoft错。对不起
dananhai 2004-05-04
  • 打赏
  • 举报
回复
enio(阿新) 是对的,这个问题别处早有了
新自由呼吸 2004-05-04
  • 打赏
  • 举报
回复
#include <iostream.h>
//using namespace std;

class Score
{
friend ostream & operator<<(ostream & os, const Score & s);
private:
unsigned int num;
public:
Score():num(0){};

};


ostream & operator<<(ostream & os, const Score & s)
{
os<<s.num;
return os;
}

请试下
julyclyde 2004-05-04
  • 打赏
  • 举报
回复
enio错
新自由呼吸 2004-05-04
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
改为
#include <iostream.h>
vcchunhong 2004-05-04
  • 打赏
  • 举报
回复
不好意思~我说错了
忘了是友元的
vcchunhong 2004-05-04
  • 打赏
  • 举报
回复
因为你的num是私有
不能用对象直接访问它
应该弄一个成员函数来访问
sdhls 2004-05-04
  • 打赏
  • 举报
回复
原来是这么回事,受益者之一。thanks
chenqing1128 2004-05-04
  • 打赏
  • 举报
回复
这个VC的问题,楼主可以去这个地方看看
http://expert.csdn.net/Expert/topic/3019/3019553.xml?temp=.4955255

64,654

社区成员

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

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