C++ 如何在另一个类中创建结构指针?
我有3个类:Book、Student、LibrarySystem,我需要3个linkedList来存储数据,第一个linkedList称为节点,用于存储学生,第二个linkedList称为studenBookNode,用于存储特定学生保留的图书,第三个linkedList是allBooksNode,用于存储创建的图书对象 以下是我的实现:C++ 如何在另一个类中创建结构指针?,c++,object,data-structures,linked-list,C++,Object,Data Structures,Linked List,我有3个类:Book、Student、LibrarySystem,我需要3个linkedList来存储数据,第一个linkedList称为节点,用于存储学生,第二个linkedList称为studenBookNode,用于存储特定学生保留的图书,第三个linkedList是allBooksNode,用于存储创建的图书对象 以下是我的实现: class Book { public: Book(); Book(int id, string title, int year);
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;
};
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;
}
}
我在LibrarySystem中有一个方法checkOutBook,我需要访问该方法中的studentBookNode,首先我要检查studentId和bookId的可用性,然后如果它们可用,我需要将该书添加到特定的studentBookNode,但由于范围问题,我无法访问它,我如何解决这个问题,以下是我的实现:
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;
};
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 was not declared in this scope
studentBookNode
是类Student
的成员。除非您是Student
类的成员,否则此名称只能作为Student::studentBookNode
访问。您知道许多书籍在其一生中可以更改其名称或版本年份吗?只是问问而已。@n.“代词是m。我无法理解你的问题。这与你的问题完全相切,但仍然。您有setBookName()
和setBookYear()
。这些函数用于什么?一位图书馆经理是否会在某一天早上醒来后决定,从现在起,“如何防弹你的小猫,2013”应该被称为“1886年,为了娱乐和利润向人们扔蔬菜”?@n.“代词”m.啊,先生,此代码用于大学作业,禁止更改获取/设置方法,我的问题是如何在LibrarySystem类中访问studenBookNode?@n.“代词m。如何修复打字错误或不正确的数据?