24小时热门版块排行榜    

Znn3bq.jpeg
查看: 427  |  回复: 4
当前主题已经存档。

soarme

银虫 (初入文坛)

[交流] 【求助】为什么不能使用私有成员【已完结】

高手们帮我看下下面这个简单的程序:
头文件new.h程序:
#ifndef _NEW_H_
#define _NEW_H_
#include
using namespace std;
class CStudent
{
private:
string strName;
int chinese;
int math;
int English;
public:
CStudent(string a,int b,int c,int d);
friend ostream& operator<<(ostream& out,CStudent S);
~CStudent(){}
};
#endif;
然后是类的实现程序,里面用到了一个输出流的重载:
#include
#include"new.h"
#include
using namespace std;
CStudent:: CStudent(string a,int b,int c,int d)
{
strName=a;
chinese=b;
math=c;
English=d;
}
ostream& operator<<(ostream& out,CStudent S)
{
cout<<"("<"< return out;
}
最后是主程序:
#include
#include"new.h"
#include
using namespace std;
int main()
{
   CStudent student1("Hector",78,88,98);
   cout< cin.get();
   return 0;
}
但结果出错显示是不能访问类的私有变量:
student.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\s1\student.cpp(14) : error C2248: 'strName' : cannot access private member declared in class 'CStudent'
        c:\program files\microsoft visual studio\myprojects\s1\new.h(8) : see declaration of 'strName'
C:\Program Files\Microsoft Visual Studio\MyProjects\s1\student.cpp(14) : error C2248: 'chinese' : cannot access private member declared in class 'CStudent'
        c:\program files\microsoft visual studio\myprojects\s1\new.h(9) : see declaration of 'chinese'
C:\Program Files\Microsoft Visual Studio\MyProjects\s1\student.cpp(14) : error C2248: 'math' : cannot access private member declared in class 'CStudent'
        c:\program files\microsoft visual studio\myprojects\s1\new.h(10) : see declaration of 'math'
C:\Program Files\Microsoft Visual Studio\MyProjects\s1\student.cpp(14) : error C2248: 'English' : cannot access private member declared in class 'CStudent'
        c:\program files\microsoft visual studio\myprojects\s1\new.h(11) : see declaration of 'English'
当我把类定义中的权限改为Public后就可以了,这是为什么啊?那个输出流函数我已经定义为友元函数了啊?

收藏 分享 评分 Sample Text


本帖关键词:int operator std math string

[ Last edited by 余泽成 on 2010-1-27 at 22:33 ]
回复此楼
I can accept failure ,but I can't accept not trying!
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

magic7004

金虫 (职业作家)

★ ★
小木虫(金币+0.5):给个红包,谢谢回帖交流
jjdg(金币+1,VIP+0):感谢交流经验! 12-4 12:39
搂住用的是VC6吧?

貌似这个是VC6的Bug,建议搂住重装英文版VC++,然后打SP6补丁,有可能能解决。

ps:我的VC6也遇到过这个问题。不过现在用VC++ 2005就OK的。
流氓不可怕,可怕的是流氓有文化,有文化又BH的流氓无敌~~!
2楼2009-12-04 11:24:02
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

soarme

银虫 (初入文坛)

波不动(金币+0,VIP+0):如果解决了的话,请及时反馈!有奖励,谢谢! 12-10 02:17
哦,是啊,校内的最新版本就下来用了,还有一些其他莫名的错误,换其他的版本试下。。。。
I can accept failure ,but I can't accept not trying!
3楼2009-12-04 18:52:59
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

breezeluo

金虫 (小有名气)

★ ★
小木虫(金币+0.5):给个红包,谢谢回帖交流
余泽成(金币+1,VIP+0):谢谢参与应助,欢迎常来程序软件版! 12-5 13:16
大概是编译器的问题吧!
象蜗牛一样狂奔
4楼2009-12-05 00:57:55
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

soarme

银虫 (初入文坛)


波不动(金币+1,VIP+0):感谢反馈,奖励一下。 12-11 20:19
恩,换了高级一点的编译器后可以了。vc6.0还是存在一些问题的。
I can accept failure ,but I can't accept not trying!
5楼2009-12-11 19:34:36
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 soarme 的主题更新
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考博] 现在不知道怎么办,感觉很痛苦 +5 qweww 2026-05-11 6/300 2026-05-16 19:57 by Equinoxhua
[基金申请] 请问大佬b0816评完了吗 +4 市民华南虎 2026-05-12 8/400 2026-05-16 19:54 by Equinoxhua
[博后之家] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +3 k37jurhrau 2026-05-16 3/150 2026-05-16 19:10 by j6b2pdz07o
[论文投稿] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +3 ky2p12rrjj 2026-05-15 4/200 2026-05-16 18:45 by j6b2pdz07o
[找工作] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +3 ky2p12rrjj 2026-05-15 3/150 2026-05-16 18:35 by j6b2pdz07o
[考研] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +3 x0mp7owy2b 2026-05-15 3/150 2026-05-16 17:55 by j6b2pdz07o
[论文投稿] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +3 x0mp7owy2b 2026-05-15 3/150 2026-05-16 17:50 by j6b2pdz07o
[硕博家园] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +4 x0mp7owy2b 2026-05-15 4/200 2026-05-16 17:45 by j6b2pdz07o
[基金申请] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +3 v9tggjlwd0 2026-05-15 3/150 2026-05-16 17:37 by j6b2pdz07o
[论文投稿] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +3 v9tggjlwd0 2026-05-15 3/150 2026-05-16 17:30 by j6b2pdz07o
[考博] 光量子物理方向 博士招生 1人(2026.09) +3 sandyworld 2026-05-15 3/150 2026-05-16 17:11 by zznnnj
[有机交流] 如何实现卤原子转化 10+3 BT20230424 2026-05-15 5/250 2026-05-16 16:20 by czyzsu
[论文投稿] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +3 xx7gd5zq4e 2026-05-15 5/250 2026-05-16 12:24 by h3oerqvkv9
[硕博家园] 申请博士 +3 呃?呃 2026-05-15 3/150 2026-05-16 11:01 by a4742549
[硕博家园] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +4 l7k6xnh0yc 2026-05-14 5/250 2026-05-16 04:29 by k37jurhrau
[考博] 西南大学考核制博士 +4 lijunjie84 2026-05-11 7/350 2026-05-15 23:20 by 同仁堂教主
[文学芳草园] 风把牡丹吹跑了 +5 myrtle 2026-05-12 9/450 2026-05-15 15:27 by myrtle
[教师之家] 教学课件你会给同学吗 +8 硕士研究生吗 2026-05-13 8/400 2026-05-14 22:23 by 常规沥青
[考博] 26应届毕业生考博求助 +3 wo一定上岸 2026-05-13 3/150 2026-05-14 21:47 by 明海天涯
[论文投稿] 求助大佬sci投稿哪个好中 +3 江沅188 2026-05-12 4/200 2026-05-13 14:35 by 江沅188
信息提示
请填处理意见