鱼C论坛

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

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

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

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

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

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

#include<iostream>
#include<string.h>
using namespace std;
class Student {                // 学生的数据域,存放学生的信息 
        public:
        int sno;
        char sname[10];
};
class NodeStudent {                // 学生链表节点 
        public:
        Student data;
        NodeStudent *next;
};
class Studentlist {
        private:
                NodeStudent* head;        // 学生链表的头 
                NodeStudent* rear;  // 学生列表的尾巴 
                int itemCount;        // 学生的数量 
        public:
                Studentlist();                                                // 初始化头指针 
                ~Studentlist();                                                // 释放学生列表 
                Student& operator[](char *name);          // 重载运算符[], 参数为字符串,返回Student引用 
                void add(Student s);                                // 增加一个学生,放入链表  
                void print();                                                // 打印所有学生 
};
Student& Studentlist::operator[](char *name) {
        NodeStudent *p(head->next);
        while(p){
                if(strcmp(p->data.sname, name) == 0) {
                        return p->data;
                }
        }
}
最佳答案
2022-3-19 15:17:55
本帖最后由 傻眼貓咪 于 2022-3-19 15:19 编辑

试试这个:
#include <iostream>
#include <cstring>

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

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

class StudentsList {
private:
        Node* head;
        size_t size;
public:
        StudentsList();
        StudentsList(Student*);
        ~StudentsList();
        Student& operator[](std::string name);
        void add(Student*);
        void print();
};

StudentsList::StudentsList() {
        head = NULL;
        size = 0;
}

StudentsList::StudentsList(Student *stu) {
        Node* tail = NULL;
        head = new Node;
        head->data = { stu->ID, stu->name };
        head->next = tail;
        size = 1;
}

StudentsList::~StudentsList() {
        Node* p;
        if (head) {
                p = head;
                head = head->next;
                delete p;
        }
        delete head;
}

Student& StudentsList::operator[](std::string name) {
        Node* p;
        if (head) {
                p = head;
                while (p) {
                        if (!(strcmp(p->data.name.c_str(), name.c_str()))) {
                                return p->data;
                        }
                        p = p->next;
                }
        }
        std::cout << "访问未找到匹配名字 (name)" << std::endl;
        static Student stu; // 静态 <------------- 注意这里 -------------
        return stu; // 返回结构体 <------------- 注意这里 -------------
        }

void StudentsList::add(Student* stu) {
        Node* tail;
        tail = head;
        head = new Node;
        head->data = { stu->ID, stu->name };
        head->next = tail;
        size++;
}

void StudentsList::print() {
        Node* p;
        if (head) {
                p = head;
                while (p) {
                        std::cout
                                << p->data.ID
                                << " "
                                << p->data.name
                                << std::endl;
                        p = p->next;
                }
        }
        else {
                std::cout << "linked list is empty" << std::endl;
        }
}

int main() {
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> 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 编辑

试试这个:
#include <iostream>
#include <cstring>

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

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

class StudentsList {
private:
        Node* head;
        size_t size;
public:
        StudentsList();
        StudentsList(Student*);
        ~StudentsList();
        Student& operator[](std::string name);
        void add(Student*);
        void print();
};

StudentsList::StudentsList() {
        head = NULL;
        size = 0;
}

StudentsList::StudentsList(Student *stu) {
        Node* tail = NULL;
        head = new Node;
        head->data = { stu->ID, stu->name };
        head->next = tail;
        size = 1;
}

StudentsList::~StudentsList() {
        Node* p;
        if (head) {
                p = head;
                head = head->next;
                delete p;
        }
        delete head;
}

Student& StudentsList::operator[](std::string name) {
        Node* p;
        if (head) {
                p = head;
                while (p) {
                        if (!(strcmp(p->data.name.c_str(), name.c_str()))) {
                                return p->data;
                        }
                        p = p->next;
                }
        }
        std::cout << "访问未找到匹配名字 (name)" << std::endl;
        static Student stu; // 静态 <------------- 注意这里 -------------
        return stu; // 返回结构体 <------------- 注意这里 -------------
        }

void StudentsList::add(Student* stu) {
        Node* tail;
        tail = head;
        head = new Node;
        head->data = { stu->ID, stu->name };
        head->next = tail;
        size++;
}

void StudentsList::print() {
        Node* p;
        if (head) {
                p = head;
                while (p) {
                        std::cout
                                << p->data.ID
                                << " "
                                << p->data.name
                                << std::endl;
                        p = p->next;
                }
        }
        else {
                std::cout << "linked list is empty" << std::endl;
        }
}

int main() {
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> 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-10-5 14:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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