0

I am implementing a doubly linked list in C++ and I am trying to add a node.append(node2) method to the node class which will link a new node object (node2) to the previous one (node). I receive an expected primary-expression before ‘;’ token error on the line node->previous = Node;. How do I get the address of the object within its class definition so that I can assign it to node->previous?

class Node {
    public:
        int value;
        Node *next;//points to next node. 
        Node *previous;//doubly linked list.
        void append(Node *node) {
            next = node;
            node->previous = Node;
            node->next = NULL;
        }
};
Patrick
  • 71
  • 5

1 Answers1

0

Use the this pointer to access the address of your object in C++. Change the line node->previous = Node; to node->previous = this;.

Patrick
  • 71
  • 5
  • 1
    Be that as it may, this code can be improved a lot. Why have an append member if you're only using it for one node? Why have a doubly-linked-list type if you're manually allocating nodes? Should you be checking for cycles? How would you do deallocatgion? Why are you including headers from `bits/`? Not even the [`using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) is required :). Here's an equivalent sample: [Live On coliru](http://coliru.stacked-crooked.com/a/741dacaea9660ac3) – sehe Jul 11 '20 at 02:20
  • @sehe Thank you for your input. I have removed the code which was unnecessary to communicate the question. – Patrick Jul 11 '20 at 02:42