0

I'm working on my final project for C++ and my professor specified that we had to have three class files for a linked list.

The first named LinkedList holds the head and the tail, as well as two overloaded operators in which we have to use the list as an array, and add an element to the end of the array.

The second named Node holds the two generic values Seat and Row.

The third and final named RNode holds the values of the next, previous spots in the list, as well as reservation status.

My problem is when using my LinkedList.cpp, defining all of the functions, I cannot figure out how to set node equal to the head, because the types are different. I can set the next node in the list with tempNode.setNext(Head);. But when I try to do tempNode = tempNode.getNext() it says the types are not the same. What is an easy way for me to make this work?

Here is my code.

This is supposed to use the Linked List as an array and return the pointer to the Node correlating with the integer passed in.

int& LinkedList::operator[] (const int &middle) {
    RNode *tempNode;
    tempNode->setNext(Head);

    tempNode = tempNode->getNext(); // Error here 

    for (int i = 0; i < middle; i++) {
    }
}

Here are the three class files I have currently made.

Linked List Class

#ifndef LINKEDLIST_H_INCLUDED
#define LINKEDLIST_H_INCLUDED

#include "Node.h"

class LinkedList {
    private:
        Node* Head; // Head of linked list
        Node* Tail; // Tail of linked list

    public:
        // Constructors
        LinkedList(); // Set default values
        LinkedList(Node*, Node*); // Set values passed in to head and tail

        int& operator [] (const int &); // Overloaded [] operator PAGE 854 HOW TO USE THIS

            // Treat list like an array.
            // First node will be [0]
            // Return pointer to node indicated inside of brackets

        Node& operator += (const Node &); // Overloaded += operator

            // Adds a node to the end of the linked list

            // Head
            void setHead(Node*); // Sets head of list
            Node* getHead(); // Returns the head of list

            // Tail
            void setTail(Node*); // Sets tail of list
            Node* getTail(); // Returns tail of list
};

#endif // LINKEDLIST_H_INCLUDED

Reservation Node Class

#ifndef RNODE_H_INCLUDED
#define RNODE_H_INCLUDED

#include "Node.h"

using namespace std;

class RNode : public Node {
    private:
        Node* Next; // Next node pointer
        Node* Prev; // Previous node pointer
        bool reservationStatus(); // Reservation status

    public:
        // Constructors
        RNode(); // Sets default values
        RNode(Node*, Node*, bool); // Takes values passed in and sets them

        // Overloaded operators
        friend ostream &operator << (ostream &, Node*); // Prints out correct symbol based on reservation status
        friend istream &operator >> (istream &, Node*); // Prints correct symbol based on reservation status  .

        // Next
        void setNext(Node*); // Sets next node in list
        Node* getNext(); // Returns next node in list

        // Prev
        void setPrev(Node*); // Sets previous node in list
        Node* getPrev();  // Returns previous node in list

        // Reservation Status
        void setReservationStatus(bool); // Sets reservation status of a current node
        bool getReservationStatus(); // Returns reservation status
};

#endif // RNODE_H_INCLUDED

Node Class

#ifndef NODE_H_INCLUDED
#define NODE_H_INCLUDED

class Node {
    protected:
        int row;
        int seat;

    public:
        // Constructors
        Node(); // Sets default values
        Node(int, int); // Sets row and seat to values passed in

        // Row
        void setRow(int); // Sets row for current node
        int getRow(); // Gets row for current node

        // Seats
        void setSeat(int); // Sets seat for current node
        int getSeat(); // Gets seat for current node
};

#endif // NODE_H_INCLUDED

In summary, how can I match the types so that I can set RNode tempNode equal to a Node? This is very confusing and I can't really find a good explanation on how to solve this.

Keep in mind, according to my instructions I have to have the classes created this way. If it were up to me, I would have combined the RNode and Node class.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 1
    Since `RNode` derives from `Node` you could downcast (using either `dynamic_cast` or `static_cast`). Though I am confused *why* it is a different type, that makes me think something is wrong with your design – UnholySheep Apr 29 '17 at 19:35
  • 2
    Side note: please [unlearn `using namespace std;`](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Quentin Apr 29 '17 at 20:14
  • @UnholySheep This is the correct answer. I asked my professor and he mentioned that I could solely use RNode in the LinkedList class rather than having to downcast. My design was wrong. Thanks for the help! – Thomas Walker Apr 29 '17 at 20:28

0 Answers0