title: Singly-Linked List Add Function - Read Access Violation
body: <p>I'm trying to create a basic singly-linked list using a separate Node class and LinkedList class. I barely know what I'm doing as I've just started learning C++, so any help would be greatly appreciated.</p>
  
  <p>The LinkedList part of the code runs on its own, but I'm sure there are some corrections to be made there too. My main problem is that, when trying to add to the linked list, I'm getting (at line 64 of LinkedList.h):</p>
  
  <blockquote>
    <p>Exception thrown: read access violation. this->head was nullptr.</p>
  </blockquote>
  
  <p>I'm using Microsoft Visual Studio 2015. Here's the code:</p>
  
  <p>LinkedList.h (it's inline):</p>
  
  <pre><code>#pragma once
  #include &lt;iostream&gt;
  
  using namespace std;
  
  class Node
  {
  private:
      Node *next = NULL;
      int data;
  
  
  
  public:
      Node(int newData) {
          data = newData;
          next = NULL;
      }
      Node() {
  
      }
      ~Node() {
          if(next)
              delete(next);
      }
      Node(int newData, Node newNext) {
          data = newData;
          *next = newNext;
      }
      void setNext(Node newNext) {
          *next = newNext;
      }
      Node getNext() {
          return *next;
      }
      int getData() {
          return data;
      }
  
  };
  
  
  class LinkedList
  {
  
  private:
      Node *head;
      int size;
  public:
  
      LinkedList()
      {
          head = NULL;
          size = 0;
      }
  
      ~LinkedList()
      {
  
      }
  
      void add(int numberToAdd)
      {
          head = new Node(numberToAdd, *head);
          ++size;
      }
  
      int remove()
      {
          if (size == 0) {
              return 0;
          }
          else {
              *head = (*head).getNext();
              --size;
              return 1;
          }
      }
  
      int remove(int numberToRemove)
      {
          if (size == 0)
              return 0;
          Node *currentNode = head;
          for (int i = 0; i &lt; size; i++) {
              if ((*currentNode).getData() == numberToRemove) {
                  *currentNode = (*currentNode).getNext();
                  return 1;
              }
          }
      }
  
      void print()
      {
          if (size == 0) {
              return;
          }
          else {
              Node currentNode = *head;
              for (int i = 0; i &lt; size; i++) {
                  cout &lt;&lt; currentNode.getData();
                  currentNode = currentNode.getNext();
              }
              cout &lt;&lt; endl;
          }
      }
  
  };
  </code></pre>
  
  <p>List Tester.cpp</p>
  
  <pre><code>    // List Tester.cpp : Defines the entry point for the console application.
  //
  
  #include "stdafx.h"
  #include &lt;iostream&gt;
  #include "LinkedList.h"
  
  using namespace std;
  
  int main()
  {
      LinkedList myList;
      myList.add(4);
      system("pause");
  }
  </code></pre>