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).
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:
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