鱼C论坛

 找回密码
 立即注册
查看: 625|回复: 5

[已解决]返回值为引用类型的函数的一个小问题

[复制链接]
发表于 2022-3-19 12:29:01 | 显示全部楼层 |阅读模式

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

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

x
如果Student& Studentlist::operator[](char *name);这个函数没能学生列表中找到匹配的学生,它会返回什么东西呢?


  1. #include<iostream>
  2. #include<string.h>
  3. using namespace std;
  4. class Student {                // 学生的数据域,存放学生的信息
  5.         public:
  6.         int sno;
  7.         char sname[10];
  8. };
  9. class NodeStudent {                // 学生链表节点
  10.         public:
  11.         Student data;
  12.         NodeStudent *next;
  13. };
  14. class Studentlist {
  15.         private:
  16.                 NodeStudent* head;        // 学生链表的头
  17.                 NodeStudent* rear;  // 学生列表的尾巴
  18.                 int itemCount;        // 学生的数量
  19.         public:
  20.                 Studentlist();                                                // 初始化头指针
  21.                 ~Studentlist();                                                // 释放学生列表
  22.                 Student& operator[](char *name);          // 重载运算符[], 参数为字符串,返回Student引用
  23.                 void add(Student s);                                // 增加一个学生,放入链表  
  24.                 void print();                                                // 打印所有学生
  25. };
  26. Student& Studentlist::operator[](char *name) {
  27.         NodeStudent *p(head->next);
  28.         while(p){
  29.                 if(strcmp(p->data.sname, name) == 0) {
  30.                         return p->data;
  31.                 }
  32.         }
  33. }
复制代码
最佳答案
2022-3-19 15:17:55
本帖最后由 傻眼貓咪 于 2022-3-19 15:19 编辑

试试这个:
  1. #include <iostream>
  2. #include <cstring>

  3. class Student {
  4. public:
  5.         int ID{}; // Warning C26495 Variable 'Student::ID' is uninitialized.Always initialize a member variable(type.6).
  6.         std::string name;
  7. };

  8. class Node {
  9. public:
  10.         Student data;
  11.         Node* next{}; // Warning C26495 Variable 'Node::next' is uninitialized.Always initialize a member variable(type.6).
  12. };

  13. class StudentsList {
  14. private:
  15.         Node* head;
  16.         size_t size;
  17. public:
  18.         StudentsList();
  19.         StudentsList(Student*);
  20.         ~StudentsList();
  21.         Student& operator[](std::string name);
  22.         void add(Student*);
  23.         void print();
  24. };

  25. StudentsList::StudentsList() {
  26.         head = NULL;
  27.         size = 0;
  28. }

  29. StudentsList::StudentsList(Student *stu) {
  30.         Node* tail = NULL;
  31.         head = new Node;
  32.         head->data = { stu->ID, stu->name };
  33.         head->next = tail;
  34.         size = 1;
  35. }

  36. StudentsList::~StudentsList() {
  37.         Node* p;
  38.         if (head) {
  39.                 p = head;
  40.                 head = head->next;
  41.                 delete p;
  42.         }
  43.         delete head;
  44. }

  45. Student& StudentsList::operator[](std::string name) {
  46.         Node* p;
  47.         if (head) {
  48.                 p = head;
  49.                 while (p) {
  50.                         if (!(strcmp(p->data.name.c_str(), name.c_str()))) {
  51.                                 return p->data;
  52.                         }
  53.                         p = p->next;
  54.                 }
  55.         }
  56.         std::cout << "访问未找到匹配名字 (name)" << std::endl;
  57.         static Student stu; // 静态 <------------- 注意这里 -------------
  58.         return stu; // 返回结构体 <------------- 注意这里 -------------
  59.         }

  60. void StudentsList::add(Student* stu) {
  61.         Node* tail;
  62.         tail = head;
  63.         head = new Node;
  64.         head->data = { stu->ID, stu->name };
  65.         head->next = tail;
  66.         size++;
  67. }

  68. void StudentsList::print() {
  69.         Node* p;
  70.         if (head) {
  71.                 p = head;
  72.                 while (p) {
  73.                         std::cout
  74.                                 << p->data.ID
  75.                                 << " "
  76.                                 << p->data.name
  77.                                 << std::endl;
  78.                         p = p->next;
  79.                 }
  80.         }
  81.         else {
  82.                 std::cout << "linked list is empty" << std::endl;
  83.         }
  84. }

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

使用道具 举报

发表于 2022-3-19 12:52:05 | 显示全部楼层

没返回值,你给他一个就是了。

Student& Studentlist::operator[](char *name) {
        NodeStudent *p(head->next);
        while(p){
                if(strcmp(p->data.sname, name) == 0) {
                        return p->data;
                }
return NULL;
        }

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

使用道具 举报

发表于 2022-3-19 15:17:55 | 显示全部楼层    本楼为最佳答案   
本帖最后由 傻眼貓咪 于 2022-3-19 15:19 编辑

试试这个:
  1. #include <iostream>
  2. #include <cstring>

  3. class Student {
  4. public:
  5.         int ID{}; // Warning C26495 Variable 'Student::ID' is uninitialized.Always initialize a member variable(type.6).
  6.         std::string name;
  7. };

  8. class Node {
  9. public:
  10.         Student data;
  11.         Node* next{}; // Warning C26495 Variable 'Node::next' is uninitialized.Always initialize a member variable(type.6).
  12. };

  13. class StudentsList {
  14. private:
  15.         Node* head;
  16.         size_t size;
  17. public:
  18.         StudentsList();
  19.         StudentsList(Student*);
  20.         ~StudentsList();
  21.         Student& operator[](std::string name);
  22.         void add(Student*);
  23.         void print();
  24. };

  25. StudentsList::StudentsList() {
  26.         head = NULL;
  27.         size = 0;
  28. }

  29. StudentsList::StudentsList(Student *stu) {
  30.         Node* tail = NULL;
  31.         head = new Node;
  32.         head->data = { stu->ID, stu->name };
  33.         head->next = tail;
  34.         size = 1;
  35. }

  36. StudentsList::~StudentsList() {
  37.         Node* p;
  38.         if (head) {
  39.                 p = head;
  40.                 head = head->next;
  41.                 delete p;
  42.         }
  43.         delete head;
  44. }

  45. Student& StudentsList::operator[](std::string name) {
  46.         Node* p;
  47.         if (head) {
  48.                 p = head;
  49.                 while (p) {
  50.                         if (!(strcmp(p->data.name.c_str(), name.c_str()))) {
  51.                                 return p->data;
  52.                         }
  53.                         p = p->next;
  54.                 }
  55.         }
  56.         std::cout << "访问未找到匹配名字 (name)" << std::endl;
  57.         static Student stu; // 静态 <------------- 注意这里 -------------
  58.         return stu; // 返回结构体 <------------- 注意这里 -------------
  59.         }

  60. void StudentsList::add(Student* stu) {
  61.         Node* tail;
  62.         tail = head;
  63.         head = new Node;
  64.         head->data = { stu->ID, stu->name };
  65.         head->next = tail;
  66.         size++;
  67. }

  68. void StudentsList::print() {
  69.         Node* p;
  70.         if (head) {
  71.                 p = head;
  72.                 while (p) {
  73.                         std::cout
  74.                                 << p->data.ID
  75.                                 << " "
  76.                                 << p->data.name
  77.                                 << std::endl;
  78.                         p = p->next;
  79.                 }
  80.         }
  81.         else {
  82.                 std::cout << "linked list is empty" << std::endl;
  83.         }
  84. }

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

使用道具 举报

 楼主| 发表于 2022-3-19 20:07:02 | 显示全部楼层
ba21 发表于 2022-3-19 12:52
没返回值,你给他一个就是了。

Student& Studentlist::operator[](char *name) {

我这边返回NULL会报错吖,我用的dev-c++
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-20 11:16:42 | 显示全部楼层
chenlifeng 发表于 2022-3-19 20:07
我这边返回NULL会报错吖,我用的dev-c++

当然会报错,这和 DEV C++ 编译器没有关系,纯粹是看你的函数返回什么类型,一般返回指针才会用到 NULL,而你的函数重载 [] 返回结构体 Student 不是指针
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-20 20:53:34 | 显示全部楼层
傻眼貓咪 发表于 2022-3-20 11:16
当然会报错,这和 DEV C++ 编译器没有关系,纯粹是看你的函数返回什么类型,一般返回指针才会用到 NULL, ...

好的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-20 19:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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