为什么不能用类的私有数据成员呢

djf102 2011-06-08 07:40:44
#include "stdafx.h"
#include<iostream>

using namespace std;

struct stu
{
char name[20];
char num[10];
char sex[4];
int age;
};


class student
{

private:
stu st;

public:
friend ostream& operator << (ostream&,student&);


};



ostream& operator << (ostream& os,student& ob)
{
os << ob.st.num<<endl;
os << ob.st.name<<endl;
os << ob.st.sex<<endl;
os << ob.st.age<<endl;
return os;
}

int main()
{

ostream& operator << (ostream& os,student& ob);

return 0;
}

出错信息:
error C2248: 'st' : cannot access private member declared in class 'student'
书上也是这么写的呀,怎样解是这个问题呢?恳请高手指教
...全文
101 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
djf102 2011-06-08
  • 打赏
  • 举报
回复
哦,你们用的都是vs呀,看来vc真old了,谢谢大家!
jiangchaomr 2011-06-08
  • 打赏
  • 举报
回复
下个VS吧 VC太老了 有的性质应该都不支持了吧
KELE2048 2011-06-08
  • 打赏
  • 举报
回复
用VS2010运行没问题的
djf102 2011-06-08
  • 打赏
  • 举报
回复
但你的是vs,我们用的是vc,vc老是出这样的错:error C2248: 'st' : cannot access private member declared in class 'student'
jiangchaomr 2011-06-08
  • 打赏
  • 举报
回复
我的编译器可以运行啊
djf102 2011-06-08
  • 打赏
  • 举报
回复
噢,vs没问题?但我现在只能用vc6,用vc6怎么改才对呢
djf102 2011-06-08
  • 打赏
  • 举报
回复
哦,试了一下,加const 后还是没改过来,还是出现同样的错误
jiangchaomr 2011-06-08
  • 打赏
  • 举报
回复
啦啦啦 谁去回答一下我的 模板与文件 问题
jiangchaomr 2011-06-08
  • 打赏
  • 举报
回复
vs
运行木有问题
ryfdizuo 2011-06-08
  • 打赏
  • 举报
回复
friend ostream& operator << (ostream&, const student&);
KID_coder 2011-06-08
  • 打赏
  • 举报
回复
visual stdio 2005
visual stdio 2008
visual stdio 2010
jiangchaomr 2011-06-08
  • 打赏
  • 举报
回复
哦 看错了 好像没什么问题 我帮你试一下 vs看
djf102 2011-06-08
  • 打赏
  • 举报
回复
那应怎样改才能碰私有变量呢?我始终不明白,改不对,谢谢各位帮忙一下!
djf102 2011-06-08
  • 打赏
  • 举报
回复
你意思是说不用友员声明吗?不用的话也不对呀
djf102 2011-06-08
  • 打赏
  • 举报
回复
我的只有vc6,但不用那个用什么呀?
jiangchaomr 2011-06-08
  • 打赏
  • 举报
回复
设为友元 私有变量当然不能碰
KID_coder 2011-06-08
  • 打赏
  • 举报
回复
别用vc6~

64,637

社区成员

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

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