Class not recognizing a member function..?

I'm following this guide:
https://www.codementor.io/codementorteam/a-comprehensive-guide-to-implementation-of-singly-linked-list-using-c_plus_plus-ondlm5azr

The issue I'm having is even though I added the function name to the class, it doesn't let the member function access private variables (from the class).

Here's my struct & class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct node {
    int data;
    node *next;
};

class list {
    private:
        node *head, *tail;
    
    public:
        // Default constructor
        list() {
            head = NULL;
            tail = NULL;
        }

        // member functions
        void createNode(int value); // creates a node and adds it to end of linked list.
        void display(); // displays all nodes in linked list.
        void insert_start(int value); // inserts a node at the start of the linked list.
        void insert_position(int pos, int value); // inserts a node at the specified position in a linked list.
};

Please note that the above is in a header file.

Then one of my problem functions from LinkedList.cpp file is as shown below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void createNode(int value) {
    node *temp = new node; 
    temp->data = value; 
    temp->next = NULL; 
    
    if (head == NULL) {
        head = temp;
        tail = temp;
        temp = NULL;
    }
    else {
        tail->next = temp;
        tail = temp;
    }
}


Errors (Note the line numbers won't be accurate):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
LinkedList.cpp: In function ‘void createNode(int)’:
LinkedList.cpp:49:9: error: ‘head’ was not declared in this scope
     if (head == NULL) {
         ^~~~
LinkedList.cpp:49:9: note: suggested alternative: ‘fread’
     if (head == NULL) {
         ^~~~
         fread
LinkedList.cpp:51:9: error: ‘tail’ was not declared in this scope
         tail = temp;
         ^~~~
LinkedList.cpp:57:9: error: ‘tail’ was not declared in this scope
         tail->next = temp; // sets the tail object's next value (pointer) to temp
         ^~~~
LinkedList.cpp: In function ‘void display()’:
LinkedList.cpp:72:12: error: ‘head’ was not declared in this scope
     temp = head;
            ^~~~
LinkedList.cpp:72:12: note: suggested alternative: ‘fread’
     temp = head;
            ^~~~
            fread
LinkedList.cpp: In function ‘void insert_start(int)’:
LinkedList.cpp:107:18: error: ‘head’ was not declared in this scope
     temp->next = head; // Assigns the address of the old head node to the "next" var of temp object
                  ^~~~
LinkedList.cpp:107:18: note: suggested alternative: ‘fread’
     temp->next = head; // Assigns the address of the old head node to the "next" var of temp object
                  ^~~~
                  fread
LinkedList.cpp: In function ‘void insert_position(int, int)’:
LinkedList.cpp:133:15: error: ‘head’ was not declared in this scope
     current = head;
               ^~~~
LinkedList.cpp:133:15: note: suggested alternative: ‘fread’
     current = head;
               ^~~~
               fread


What am I doing wrong? Thanks!


Please Note: If you really want the full code, I'll provide the files in the links below and I won't remove my comments so the error line numbers will match up to the actual lines having problems.
Main.cpp: http://cpp.sh/9fycs
LinkedList.h: http://cpp.sh/3wu6i
LinkedList.cpp: http://cpp.sh/9bbfe
Last edited on
You haven't coded it as a "member" function.

The definition should start
void list::createNode( int value )

i.e. you need the list:: or it won't be a member of anything.
Oh right, I always forget about that...

Thank you!
Topic archived. No new replies allowed.