| 查看: 427 | 回复: 4 | |||
| 当前主题已经存档。 | |||
[交流]
【求助】为什么不能使用私有成员【已完结】
|
|||
|
高手们帮我看下下面这个简单的程序: 头文件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<<"("< } 最后是主程序: #include #include"new.h" #include using namespace std; int main() { CStudent student1("Hector",78,88,98); cout< 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 ] |
» 猜你喜欢
现在不知道怎么办,感觉很痛苦
已经有6人回复
请问大佬b0816评完了吗
已经有8人回复
本人最近太闲了,谁有问题可以提,每天会统一回复
已经有20人回复
重磅!青年科学基金项目(C类)资助增幅预计超过50%
已经有8人回复
求助2,4-二氯-5-嘧啶甲醛的合成方法
已经有6人回复
评审感受-评审感受-评审感受
已经有19人回复
售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急
已经有3人回复
售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急
已经有4人回复
售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急
已经有3人回复
售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急
已经有3人回复


2楼2009-12-04 11:24:02

3楼2009-12-04 18:52:59
breezeluo
金虫 (小有名气)
- 应助: 1 (幼儿园)
- 金币: 1144.4
- 帖子: 159
- 在线: 31.2小时
- 虫号: 914733
- 注册: 2009-11-27
- 性别: GG
- 专业: 数理统计

4楼2009-12-05 00:57:55

5楼2009-12-11 19:34:36












回复此楼
10