saitcanbaskol
saitcanbaskol

Reputation: 15

How to create an struct pointer in another class?

I have 3 classes: Book,Student,LibrarySystem and i need 3 linkedlists to store data, first linkedList is called Node which stores Students, second linkedList is called studenBookNode which stores the books reserved by specific student and my third linkedList is allBooksNode which stores the created book objects here is my implementation below:

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;
};

I have a method checkOutBook in LibrarySystem and i need to access studentBookNode in the method, first im checking the availability of studentId and bookId's and then if they are available i need to add the book to the specific students studentBookNode but i could not access it because of the scope how can i fix this, here is my implementation below:

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;
    }
}

I tried to create a pointer to studentBookNode as:

studentBookNode* temp = new studentBookNode();

And the error message is

studentBookNode was not declared in this scope

Upvotes: 0

Views: 116

Answers (1)

n. m. could be an AI
n. m. could be an AI

Reputation: 120079

studentBookNode is a member of class Student. Unless you are writing a member of class Student, this name can be only accessed as Student::studentBookNode.

Upvotes: 1

Related Questions