如何在另一个类中创建结构指针



我有三个类:Book、Student、LibrarySystem,我需要三个链表来存储数据,第一个链表被称为Node,用于存储Students,第二个链表被称作studentBookNode,用于存储特定学生预订的书籍,第三个链表是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

studentBookNode是类Student的成员。除非您正在编写类Student的成员,否则只能以Student::studentBookNode的形式访问此名称。

最新更新