24小时热门版块排行榜    

北京石油化工学院2026年研究生招生接收调剂公告
查看: 411  |  回复: 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的回帖

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的回帖
查看全部 5 个回答

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的回帖

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的回帖
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 321求调剂 +6 璞玉~~ 2026-03-25 7/350 2026-03-28 17:48 by 璞玉~~
[考研] 压国家一区线,求导师收留,有恩必谢! +7 迷人的哈哈 2026-03-28 7/350 2026-03-28 16:47 by 催化大白
[考研] 一志愿北京工业大学,324分求调剂 +4 零八# 2026-03-28 4/200 2026-03-28 15:01 by 17865157980
[考研] 283求调剂 +7 A child 2026-03-28 7/350 2026-03-28 12:05 by zllcz
[考研] 0856,材料与化工321分求调剂 +12 大馋小子 2026-03-27 13/650 2026-03-28 10:56 by self2008
[考研] 266求调剂 +11 阳阳哇塞 2026-03-27 12/600 2026-03-27 17:56 by yu221
[考研] 考研调剂 +4 Sanmu-124 2026-03-26 4/200 2026-03-27 17:49 by kiokin
[考研] 一志愿北京化工大学 070300 学硕 336分 求调剂 +9 vv迷 2026-03-22 9/450 2026-03-27 15:59 by 不吃魚的貓
[考研] 07化学280分求调剂 +10 722865 2026-03-23 10/500 2026-03-27 15:51 by Plutoqq
[考研] 085600,材料与化工321分,求调剂 +9 大馋小子 2026-03-27 9/450 2026-03-27 14:30 by mmm just
[考研] 286求调剂 +4 lim0922 2026-03-26 4/200 2026-03-27 10:28 by guoweigw
[考研] 343求调剂 +4 赠我一本书 2026-03-23 4/200 2026-03-27 00:40 by wxiongid
[考研] 085602 289分求调剂 +8 WWW西西弗斯 2026-03-24 8/400 2026-03-26 16:33 by 不吃魚的貓
[考研] 086000生物与医药292求调剂 +6 小小陈小小 2026-03-22 9/450 2026-03-26 15:58 by dick_runner
[考研] 环境专硕324分求调剂推荐 +5 轩小宁—— 2026-03-26 5/250 2026-03-26 12:05 by i_cooler
[考研] 材料调剂 +3 iwinso 2026-03-23 3/150 2026-03-25 11:29 by greychen00
[考研] 086003食品工程求调剂 +6 淼淼111 2026-03-24 6/300 2026-03-25 10:29 by 3Strings
[考研] 一志愿河北工业大学0817化工278分求调剂 +7 jhybd 2026-03-23 12/600 2026-03-24 09:03 by jhybd
[考研] 284求调剂 +3 yanzhixue111 2026-03-23 6/300 2026-03-23 22:58 by pswait
[考研] 石河子大学(211、双一流)硕博研究生长期招生公告 +3 李子目 2026-03-22 3/150 2026-03-22 21:01 by 怎么释怀
信息提示
请填处理意见