鱼C论坛

 找回密码
 立即注册
查看: 1195|回复: 3

[已解决]c++友元函数初学习

[复制链接]
发表于 2024-9-24 20:43:49 | 显示全部楼层 |阅读模式

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

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

x
Compute类
  1. #pragma once
  2. #include "string"
  3. #include "ComputerService.h"

  4. using namespace std;

  5. class ComputerService;
  6. class Compute
  7. {
  8. public:
  9.         Compute();
  10.         string description();

  11. private:
  12.         string cpu;
  13.         friend void ComputerService::upgrade(Compute* compute);
  14. };

  15. *****************
  16. #include "Compute.h"
  17. #include <sstream>
  18. #include "ComputerService.h"

  19. Compute::Compute()
  20. {
  21.         this->cpu = "i7";
  22. }

  23. string Compute::description()
  24. {
  25.         stringstream ret;
  26.         ret << "CPU = " << cpu;
  27.         return ret.str();
  28. }
复制代码


ComputerService类

  1. #pragma once
  2. #include "Compute.h"
  3. class Compute;
  4. class ComputerService
  5. {
  6. public:
  7.         ComputerService();
  8.         void upgrade(Compute* compute);
  9. };
  10. *******************************
  11. #include "ComputerService.h"
  12. #include "Compute.h"

  13. ComputerService::ComputerService()
  14. {
  15. }

  16. void ComputerService::upgrade(Compute* compute)
  17. {
  18.         compute->cpu = "i9";
  19. }
复制代码


主函数
  1. #include <iostream>
  2. #include "string"
  3. #include<Windows.h>
  4. #include <fstream>
  5. #include  <sstream>
  6. #include "Compute.h"
  7. #include "ComputerService.h"
  8. using namespace std;


  9. int main(void) {
  10.         Compute compute;
  11.         ComputerService service;
  12.         cout << compute.description() << endl;

  13.         service.upgrade(&compute);

  14.         cout << compute.description() << endl;


  15.         system("pause");
  16.         return 0;
  17. }

复制代码


报错显示
捕获.JPG
最佳答案
2024-9-24 21:01:35
猪猪虾 发表于 2024-09-24 21:01
为什么我的程序报错显示,使用了未定义类型“Computer ...


鱼油你好,根据你的描述,我发现问题可能和友元函数的声明及包含关系有关。这一般是由于前向声明导致的问题,因为编译器在处理 `ComputerService::upgrade` 函数时未找到其定义。我们需要处理头文件包含的先后顺序。

下面是经过修改后可行的代码示例:

### Compute.h
  1. #pragma once
  2. #include <string>

  3. using namespace std;

  4. class ComputerService;

  5. class Compute {
  6. public:
  7.     Compute();
  8.     string description();

  9. private:
  10.     string cpu;
  11.     friend class ComputerService; // 声明为友元类即可,无需特定函数
  12. };
复制代码


### Compute.cpp
  1. #include "Compute.h"
  2. #include <sstream> // 包含 sstream 头文件
  3. #include "ComputerService.h"

  4. Compute::Compute() {
  5.     this->cpu = "i7";  // 构造函数初始化 cpu 为 "i7"
  6. }

  7. string Compute::description() {
  8.     stringstream ret;
  9.     ret << "CPU: " << cpu;
  10.     return ret.str();
  11. }
复制代码


### ComputerService.h
  1. #pragma once
  2. #include "Compute.h"

  3. class Compute;

  4. class ComputerService {
  5. public:
  6.     void upgrade(Compute* compute);
  7. };
复制代码


### ComputerService.cpp
  1. #include "ComputerService.h"
  2. #include "Compute.h"

  3. void ComputerService::upgrade(Compute* compute) {
  4.     // 可以访问 Compute 类的私有成员
  5.     compute->cpu = "i9";
  6. }
复制代码


### main.cpp
  1. #include <iostream>
  2. #include "Compute.h"
  3. #include "ComputerService.h"

  4. using namespace std;

  5. int main() {
  6.     Compute myComputer;
  7.     ComputerService service;
  8.    
  9.     cout << "Before upgrade: " << myComputer.description() << endl;
  10.     service.upgrade(&myComputer);
  11.     cout << "After upgrade: " << myComputer.description() << endl;

  12.     return 0;
  13. }
复制代码


### 说明:
1. `Compute.h` 中声明整个 `ComputerService` 类为友元。
2. `Compute.cpp` 和 `ComputerService.cpp` 各自在合适的地方包含对应的头文件。
3. 使用 `main.cpp` 测试代码。

这样修改后,你的程序应该不会再出现未定义类型的错误啦。希望这能帮到你!如果还有问题,请随时问我。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-9-24 20:50:07 | 显示全部楼层
鱼油你好,关于你的代码问题,似乎你想了解如何使用友元函数。在 C++ 中,友元函数是可以访问类的私有成员的函数。以下是对你提供的代码进行完善的一种方式:

### Compute 类定义(Compute.h)
  1. #pragma once
  2. #include "string"
  3. #include "ComputerService.h"

  4. using namespace std;

  5. class ComputerService;

  6. class Compute {
  7. public:
  8.     Compute();
  9.     string description();

  10. private:
  11.     string cpu;
  12.     friend void ComputerService::upgrade(Compute* compute);
  13. };
复制代码


### Compute 类实现(Compute.cpp)
  1. #include "Compute.h"
  2. #include <sstream> // 需要包含 sstream 头文件
  3. #include "ComputerService.h"

  4. Compute::Compute() {
  5.     this->cpu = "i7";  // 构造函数初始化 cpu 为 "i7"
  6. }

  7. string Compute::description() {
  8.     stringstream ret;
  9.     ret << "CPU: " << cpu;
  10.     return ret.str();
  11. }
复制代码


### ComputerService 类定义(ComputerService.h)
  1. #pragma once
  2. #include "Compute.h"

  3. class ComputerService {
  4. public:
  5.     void upgrade(Compute* compute);
  6. };
复制代码


### ComputerService 类实现(ComputerService.cpp)
  1. #include "ComputerService.h"

  2. void ComputerService::upgrade(Compute* compute) {
  3.     if (compute) {
  4.         compute->cpu = "i9";  // 升级 CPU
  5.     }
  6. }
复制代码


### 主函数(main.cpp)
  1. #include <iostream>
  2. #include "Compute.h"
  3. #include "ComputerService.h"

  4. int main() {
  5.     Compute myComputer;
  6.     ComputerService service;

  7.     std::cout << "Before upgrade: " << myComputer.description() << std::endl;

  8.     service.upgrade(&myComputer);

  9.     std::cout << "After upgrade: " << myComputer.description() << std::endl;

  10.     return 0;
  11. }
复制代码


### 编译与执行
确保所有文件放在同一个项目目录下并编译运行。你将看到类似如下输出:
  1. Before upgrade: CPU: i7
  2. After upgrade: CPU: i9
复制代码


希望这些解释和代码改进能帮你理解 C++ 中的友元函数用法!如果有其他问题,欢迎继续提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-9-24 21:01:09 | 显示全部楼层
FishC 发表于 2024-9-24 20:50
鱼油你好,关于你的代码问题,似乎你想了解如何使用友元函数。在 C++ 中,友元函数是可以访问类的私有成员 ...

为什么我的程序报错显示,使用了未定义类型“ComputerService”        Project1        I:\c及C++练习及混编,GPU加速-23-6-15\Project1\Compute.h        17       
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-9-24 21:01:35 | 显示全部楼层    本楼为最佳答案   
猪猪虾 发表于 2024-09-24 21:01
为什么我的程序报错显示,使用了未定义类型“Computer ...


鱼油你好,根据你的描述,我发现问题可能和友元函数的声明及包含关系有关。这一般是由于前向声明导致的问题,因为编译器在处理 `ComputerService::upgrade` 函数时未找到其定义。我们需要处理头文件包含的先后顺序。

下面是经过修改后可行的代码示例:

### Compute.h
  1. #pragma once
  2. #include <string>

  3. using namespace std;

  4. class ComputerService;

  5. class Compute {
  6. public:
  7.     Compute();
  8.     string description();

  9. private:
  10.     string cpu;
  11.     friend class ComputerService; // 声明为友元类即可,无需特定函数
  12. };
复制代码


### Compute.cpp
  1. #include "Compute.h"
  2. #include <sstream> // 包含 sstream 头文件
  3. #include "ComputerService.h"

  4. Compute::Compute() {
  5.     this->cpu = "i7";  // 构造函数初始化 cpu 为 "i7"
  6. }

  7. string Compute::description() {
  8.     stringstream ret;
  9.     ret << "CPU: " << cpu;
  10.     return ret.str();
  11. }
复制代码


### ComputerService.h
  1. #pragma once
  2. #include "Compute.h"

  3. class Compute;

  4. class ComputerService {
  5. public:
  6.     void upgrade(Compute* compute);
  7. };
复制代码


### ComputerService.cpp
  1. #include "ComputerService.h"
  2. #include "Compute.h"

  3. void ComputerService::upgrade(Compute* compute) {
  4.     // 可以访问 Compute 类的私有成员
  5.     compute->cpu = "i9";
  6. }
复制代码


### main.cpp
  1. #include <iostream>
  2. #include "Compute.h"
  3. #include "ComputerService.h"

  4. using namespace std;

  5. int main() {
  6.     Compute myComputer;
  7.     ComputerService service;
  8.    
  9.     cout << "Before upgrade: " << myComputer.description() << endl;
  10.     service.upgrade(&myComputer);
  11.     cout << "After upgrade: " << myComputer.description() << endl;

  12.     return 0;
  13. }
复制代码


### 说明:
1. `Compute.h` 中声明整个 `ComputerService` 类为友元。
2. `Compute.cpp` 和 `ComputerService.cpp` 各自在合适的地方包含对应的头文件。
3. 使用 `main.cpp` 测试代码。

这样修改后,你的程序应该不会再出现未定义类型的错误啦。希望这能帮到你!如果还有问题,请随时问我。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-7 12:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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