【发布时间】:2020-12-07 20:36:47
【问题描述】:
我有 3 个类:Book、Student、LibrarySystem,我需要 3 个链接列表来存储数据,第一个链接列表称为节点,存储学生,第二个链接列表称为 studenBookNode,存储特定学生保留的书籍,我的第三个链接列表是 allBooksNode它存储创建的书籍对象 下面是我的实现:
class Book {
public:
Book();
Book(int id, string title, int year);
~Book();
//some getter and setter functions
private:
int bookId;
string bookName;
int bookYear;
bool status;
};
class Student {
public:
Student();
Student(int id, string name);
~Student();
Student(const Student& rightVal);
//Student& operator=(const Student& rightVal);
int getId();
void setId(int id);
string getName();
void setName(string name);
//studentBookNode methods
bool isEmpty() const;
int getLength() const;
bool removeNode(int index);
int findIndex(int studentId);
struct studentBookNode {
Book item;
studentBookNode* next;
};
studentBookNode* stuhead;
int studentBooks;
studentBookNode* findPointer(int index) const;
private:
int studentId;
string studentName;
};
class LibrarySystem {
public:
LibrarySystem();
~LibrarySystem();
void addStudent(const int studentId, const string studentName);
void addBook(const int bookId, const string bookName, const int bookYear);
void deleteBook(const int bookId);
void deleteStudent(const int studentId);
void checkoutBook(const int bookId, const int studentId);
void returnBook(const int bookId);
void showAllBooks() const;
void showBook(const int bookId) const;
void showStudent(const int studentId) const;
bool isEmpty() const;
bool isBooksEmpty() const;
bool removeNode(int index);
bool removeBookNode(int index);
int findBookIndex(int bookId);
int findIndex(int studentId);
int getLengthBooks() const;
int getLength() const;
private:
struct Node {
Student item;
Node* next;
};
Node* head;
int Size;
Node* findPointer(int index) const;
struct allBooksNode {
Book item;
allBooksNode* next;
};
allBooksNode* bookHead;
int bookSize;
allBooksNode* findBookPointer(int index) const;
};
我在 LibrarySystem 中有一个 checkOutBook 方法,我需要在该方法中访问 studentBookNode,首先我检查 studentId 和 bookId 的可用性,然后如果它们可用,我需要将这本书添加到特定的学生 studentBookNode 但我不能由于范围而访问它我该如何解决这个问题,下面是我的实现:
void LibrarySystem::checkoutBook(const int bookId, const int studentId)
{
bool bookExist = false;
bool studentExist = false;
Node* cur = head;
int stuIndex;
int bookIndex;
allBooksNode* curBook = bookHead;
for (int i = 1; i <= getLengthBooks(); i++) {
if (curBook->item.getBookId() == bookId) {
bookExist = true;
bookIndex = i;
}
curBook = curBook->next;
}
if (bookExist) {
for (int i = 1; i <= getLength(); i++) {
if (cur->item.getId() == studentId) {
studentExist = true;
stuIndex = i;
}
cur = cur->next;
}
}
else {
cout << "Book :" << bookId << " does not exist for checkout" << endl;
}
if (bookExist == true && studentExist == true) {
Node* stuTemp = findPointer(stuIndex);
allBooksNode* bookTemp = findBookPointer(bookIndex);
if (stuTemp->item.getLength() == 0) {
//problem is here i cant create studentBookNode* here
studentBookNode* temp = new studentBookNode();
}
}
else {
cout << "Student: " << studentId << " does not exist for checkout" << endl;
}
}
我试图创建一个指向 studentBookNode 的指针:
studentBookNode* temp = new studentBookNode();
错误信息是
studentBookNode was not declared in this scope
【问题讨论】:
-
你知道有多少书籍可以在其一生中更改名称或版本年份吗?只是问问。
-
@n.'pronouns'm。我无法理解你的问题
-
这与您的问题完全相关,但仍然如此。你有
setBookName()和setBookYear()。这些功能是干什么用的?图书馆经理是否可以在一天早上醒来并决定从现在开始“如何为你的小猫防弹,2013 年”应该被称为“为了乐趣和利润向人们扔蔬菜,1886 年”? -
@n.'pronouns'm.Ah 先生,此代码用于大学作业,禁止更改 get/set 方法,我的问题是如何访问 LibrarySystem 类中的 studenBookNode?跨度>
-
@n.'pronouns'm。如何修复拼写错误或不正确的数据?
标签: c++ object data-structures linked-list