关于 友元 重载 operator<< ();不能访问类的私有数据的问题..再线等答案..谢谢..

Dugowe 2006-05-09 11:18:22
#ifndef mytime_h_
#define mytime_h_
#include <iostream>
using namespace std;

class CTime
{
private:
int m_hours;
int m_minutes;
public:
.....
friend ostream & operator << (ostream & os, const CTime & st);
};

#endif
-----------------------------------------------------------------------
//friend mothed
ostream & operator << (ostream & os, const CTime & st)
{
os << st.m_hours << " hours, " << st.m_minutes << "minutes." << endl;

return os;
}
-------------------------------------------------------------------------
编译错误.....
--------------------Configuration: 11_2 - Win32 Debug--------------------
Compiling...
mytime.cpp
g:\vc++6.0\answertoc++primerplus\11_2\mytime.cpp(69) : error C2248: 'm_hours' : cannot access private member declared in class 'CTime'
g:\vc++6.0\answertoc++primerplus\11_2\mytime.h(10) : see declaration of 'm_hours'
g:\vc++6.0\answertoc++primerplus\11_2\mytime.cpp(69) : error C2248: 'm_minutes' : cannot access private member declared in class 'CTime'
g:\vc++6.0\answertoc++primerplus\11_2\mytime.h(11) : see declaration of 'm_minutes'
Error executing cl.exe.

mytime.obj - 2 error(s), 0 warning(s)

========================================================================
不明白了 我的代码到底那里出了问题,用友元居然说不能访问类的私有数据....
如果把friend ostream & operator << (ostream & os, const CTime & st);再类里写成内联就不会出现编译错误.

public:
ostream & operator << (ostream & os, const CTime & st)
{
os << st.m_hours << " hours, " << st.m_minutes << "minutes." << endl;

return os;
}
----------------------------------

请大家指点...谢谢..

...全文
137 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
吃狼的豆腐 2006-05-10
  • 打赏
  • 举报
回复
up
Dugowe 2006-05-10
  • 打赏
  • 举报
回复
o 呵呵
明白了..
herman~~ 2006-05-10
  • 打赏
  • 举报
回复
不要用vc6.0写任何模板泛型程序.会浪费你很多时间来找不是错误的错误


学习
kingsun555 2006-05-10
  • 打赏
  • 举报
回复
我想也是吧. VC6有很多是不支持的.
塘外人 2006-05-09
  • 打赏
  • 举报
回复
不要用vc6.0写任何模板泛型程序.会浪费你很多时间来找不是错误的错误

用高点的版本啊
bruceteen 2006-05-09
  • 打赏
  • 举报
回复
解压 -> 安装
Dugowe 2006-05-09
  • 打赏
  • 举报
回复
o 是的...我刚下了 可是不能安装..5555555555
直接解压就可以吗??
bruceteen 2006-05-09
  • 打赏
  • 举报
回复
估计你用的是VC++6.0,而且又没有安装补丁 vssp6
sankt 2006-05-09
  • 打赏
  • 举报
回复
以前遇到过这种情况.
也是这样解决的
雨中的月儿 2006-05-09
  • 打赏
  • 举报
回复
哈哈,是啊,深有体会哦~~~

65,206

社区成员

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

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