#include<iostream> #include<string> using namespace std; typedef int Status; typedef struct MenLNode { string menID; string menName; //borrowBook m[3]; int times; struct borrowBookLNode *borrowBook; struct MenLNode* next; }MenLNode, *MenLinkList; //Men w[10000000] = {}; //结构体里面搞一个链表,结构体里面有借阅图书数量 typedef struct borrowBookLNode//改成链表 { string bookName; string bookID; string bookLei; string borrowTime; string returnTime; struct borrowBookLNode* next; }borrowBookLNode, *borrowBookLinkList; //borrowBook m[3] ; int main() { MenLinkList M = new MenLNode; M->next = NULL; borrowBookLinkList B = new borrowBookLNode; B->next = NULL; M->menID = "001"; M->menName = "姚苏晏"; M->times = 1; M->borrowBook->bookName = "《0110》"; M->borrowBook->bookID = "0001"; M->borrowBook->bookLei = "文化"; M->borrowBook->borrowTime = "2023年6月6日"; M->borrowBook->returnTime = "2023年6月20日"; //borrowBookLNode borrowBook = { bookName,bookID,bookLei,borrowTime,returnTime }; //w[0] = { menID,menName,m[1] }; cout << M->menID << endl; cout << M->menName << endl; cout << M->times << endl; cout << M->borrowBook->bookName << endl; cout << M->borrowBook->bookID << endl; cout << M->borrowBook->bookLei << endl; cout << M->borrowBook->borrowTime << endl; cout << M->borrowBook->returnTime << endl; return 0; }
时间: 2024-03-12 09:43:38 浏览: 79
这段代码有一个问题,即结构体 `MenLNode` 中的 `borrowBook` 变量是一个指向 `borrowBookLNode` 结构体的指针,但是在创建 `MenLNode` 结构体时并没有为 `borrowBook` 分配内存,所以访问 `borrowBook` 的成员时会出现错误。正确的做法是在创建 `MenLNode` 结构体时为 `borrowBook` 分配内存,可以使用 `new` 运算符来实现:
```
MenLinkList M = new MenLNode;
M->next = NULL;
M->borrowBook = new borrowBookLNode; // 分配 borrowBook 结构体的内存
M->borrowBook->next = NULL;
```
阅读全文