Issues with Nodes and Linked Lists

I have a purpose where I have to create methods for inserting and deleting nodes in a doubly linked list. However, I'm a little rusty with my C ++. I get an error message from my front and back pointers.

LinkedList.h

#ifndef LinkedList_h
#define LinkedList_h

#include <iostream>

using namespace std;

struct node {
    node * prev;
    int data;
    node * next;

};

class LinkedList {

private:
    //pointers to point to front and end of Linked List
    static node * front; //the error is coming from here
    static node * rear;  //the error is coming from here
public:
    static void insert_front(int data);
};
#endif

LinkedList.cpp

#include "LinkedList.h"

//insert int to front
void LinkedList::insert_front(int data) {

    node *q = nullptr;
    //If the list is empty
    if (front == nullptr && rear == nullptr) {
        q = new node;
        q->prev = nullptr;
        q->data = data;
        q->next = nullptr;
        front = q;
        rear = q;
        q = nullptr;
    }
    //If there is only one node in list
    //...
    //If there are at least 2 nodes in list
    //...

}

The errors I get are the following:

unresolved external symbol "private: static struct node * LinkedList::front (?front@LinkedList@@0PAUnode@@A)


unresolved external symbol "private: static struct node * LinkedList::rear (?rear@LinkedList@@0PAUnode@@A)

if I delete static data from private variables when I refer to them in the cpp file, I get a "link to a non-static element link to a specific object"

+4
source share
2 answers

front rear static. , LinkedList .

, , .cpp , @Soeren :

node* LinkedList::front = nullptr;
node* LinkedList::read = nullptr;

, , LinkedList s front rear . , ( insert_front() ). ​​

, :

LinkedList list;
list.insert_front(5);
+9

cpp:

node* LinkedList::front = nullptr;
node* LinkedList::rear = nullptr;

, . , . - , cpp.

- , , (, LinkedList:: front).

+6

Source: https://habr.com/ru/post/1668204/


All Articles