鱼C论坛

 找回密码
 立即注册
查看: 927|回复: 4

[已解决]友元 ,在类Building里面已经声明多了,visit1函数是其友元,还是无法访问私有

[复制链接]
发表于 2022-1-17 19:43:20 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 猪猪虾 于 2022-1-17 19:47 编辑

还是无法访问私有成员

  1. //全局函数做友元

  2. #include <stdio.h>
  3. #include<iostream>
  4. #include<cstring>
  5. #include<cstdio>

  6. using namespace std;

  7. class Building
  8. {
  9.         friend void GoodGay::visit1();
  10. public:
  11.         Building()
  12.         {
  13.                 m_sittingRoom = "客厅";
  14.                 m_bedRoom = "卧室";
  15.         }

  16.         string m_sittingRoom;

  17. private:
  18.         string m_bedRoom;
  19. };


  20. class GoodGay
  21. {
  22. public:
  23.         Building* building;
  24.         GoodGay()
  25.         {
  26.                 building = new Building;  //new什么就返回什么,创建了一个Building指针
  27.         }

  28.         void visit1()  //让这个函数可以访问building的私有成员
  29.         {
  30.                 cout << "visit1 is visting :" << building->m_sittingRoom << endl;
  31.                 cout << "visit1 is visting :" << building->m_bedRoom << endl;
  32.         }

  33.         void vist2()//让这个函数不可以访问building的私有成员
  34.         {
  35.                 cout << "visit2 is visting :" << building->m_sittingRoom << endl;
  36.         }
  37. };


  38. int main()
  39. {
  40.         GoodGay gay;
  41.         gay.visit1();
  42.         gay.vist2();
  43.         return 0;
  44. }
复制代码
最佳答案
2022-1-19 21:37:10
猪猪虾 发表于 2022-1-19 21:20
你这是在类外面实现成员函数,我是在类内。和声明与否没关系把

你注意你的 第 12 行 代码,系统根本不知道 GoodGay 从哪里来,因为还没有声明,直到第 31 行才出现,不能先吃饭才煮饭啊大哥。

你的问题如同:
  1. #include <iostream>
  2. using namespace std;

  3. int A(int a, int b){
  4.     return B(a, b); // 系统根本不懂 B() 是什么
  5. }

  6. int B(int a, int b){ // 这里才开始声明定义 B()
  7.     return a*b;
  8. }

  9. int main()
  10. {
  11.     return 0;
  12. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-1-17 20:58:20 | 显示全部楼层
本帖最后由 傻眼貓咪 于 2022-1-17 21:19 编辑

你的思路基本没有问题,但是声明和定义循序有问题,你要确定哪里一个优先声明和定义:(注:这里用提前声明 Forward declaration 方法,不然报错)
  1. #include <iostream>
  2. using namespace std;

  3. class Building; // 注意这里,提前声明 Forward declaration

  4. class GoodGay // 注意这里
  5. {
  6. public:
  7.     void visit1(); // 注意这里,提前声明 Forward declaration
  8.     void vist2();  // 注意这里,提前声明 Forward declaration
  9.     GoodGay();     // 构造函数声明
  10.     ~GoodGay();    // 析构函数声明
  11.     Building* building;
  12. };

  13. class Building // 注意这里
  14. {
  15.     public:
  16.         Building()
  17.         {
  18.             m_sittingRoom = "客厅";
  19.             m_bedRoom = "卧室";
  20.         }
  21.         string m_sittingRoom;
  22.         friend void GoodGay::visit1();
  23.     private:
  24.         string m_bedRoom;
  25. };

  26. GoodGay::GoodGay() // 注意这里,必须在定义 class Building 之后才能定义
  27. {
  28.     building = new Building;
  29. }

  30. GoodGay::~GoodGay()
  31. {
  32.     delete building; // 凡是有 new,必然要有 delete
  33. }

  34. void GoodGay::visit1() // 注意这里,必须在定义 class Building 之后才能定义
  35. {
  36.     cout << "visit1 is visting :" << building->m_sittingRoom << endl;
  37.     cout << "visit1 is visting :" << building->m_bedRoom << endl;
  38. }

  39. void GoodGay::vist2() // 注意这里,必须在定义 class Building 之后才能定义
  40. {
  41.     cout << "visit2 is visting :" << building->m_sittingRoom << endl;
  42. }

  43. int main(){
  44.     GoodGay gay;
  45.     gay.visit1();
  46.     gay.vist2();
  47.     return 0;
  48. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-1-19 21:20:22 | 显示全部楼层
傻眼貓咪 发表于 2022-1-17 20:58
你的思路基本没有问题,但是声明和定义循序有问题,你要确定哪里一个优先声明和定义:(注:这里用提前声明 ...

你这是在类外面实现成员函数,我是在类内。和声明与否没关系把
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-19 21:31:48 | 显示全部楼层
猪猪虾 发表于 2022-1-19 21:20
你这是在类外面实现成员函数,我是在类内。和声明与否没关系把

大哥,写代码很辛苦,尤其是特长代码,希望你认真看清楚。

我拿其中一个例子,你的 GoodGay() 是 GoodGay 的构造函数,在 GoodGay 里面声明了,但是必须 Building 之后才能定义。
你的代码牵涉很多声明和定义顺序。

如果用你的代码执行,报错已经说明一切!
问题.jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-19 21:37:10 | 显示全部楼层    本楼为最佳答案   
猪猪虾 发表于 2022-1-19 21:20
你这是在类外面实现成员函数,我是在类内。和声明与否没关系把

你注意你的 第 12 行 代码,系统根本不知道 GoodGay 从哪里来,因为还没有声明,直到第 31 行才出现,不能先吃饭才煮饭啊大哥。

你的问题如同:
  1. #include <iostream>
  2. using namespace std;

  3. int A(int a, int b){
  4.     return B(a, b); // 系统根本不懂 B() 是什么
  5. }

  6. int B(int a, int b){ // 这里才开始声明定义 B()
  7.     return a*b;
  8. }

  9. int main()
  10. {
  11.     return 0;
  12. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-20 17:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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