鱼C论坛

 找回密码
 立即注册
查看: 2936|回复: 2

有關构造函数的問題

[复制链接]
发表于 2013-6-6 18:18:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
想問一下以下代碼為什麼有錯誤? 我是照著小甲魚的<<訪問控制>>的課件來打的。

|11|error: 'std::string Person::name' is protected|
|33|error: within this context|
||=== Build finished: 2 errors, 0 warnings (0 minutes, 0 seconds) ===|



  1. #include <iostream>
  2. #include <string>

  3. using namespace std;

  4. class Person
  5. {
  6. public:
  7.         Person(string AIname);
  8. protected:
  9.         string name;
  10. };

  11. class Pig : public Person
  12. {
  13. public:
  14.         Pig(string AIname);
  15. };

  16. Person::Person(string AIname)
  17. {
  18.         name = AIname;
  19. }

  20. Pig::Pig(string AIname) : Person(AIname)
  21. {
  22. }

  23. int main()
  24. {
  25.         Pig pig("pig");

  26.         cout << pig.name <<endl; //這句有問題

  27.         return 0;
  28. }
复制代码



想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-6-6 19:35:16 | 显示全部楼层
protected:
        string name;
name是protected属性,外界是无法直接访问的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-6-6 20:03:20 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-2 16:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表