title: C++ Access violation reading location in linked list
body: <p>I have lately been trying to solve <a href="https://www.reddit.com/r/dailyprogrammer/comments/3a64hq/20150617_challenge_219_intermediate_todo_list/" rel="nofollow">this reddit challenge</a>(problem's part 2):</p>
  
  <blockquote>
    <h2>Description</h2>
    
    <p>Thanks for that list you made me, my thoughts are way more organised!
    I've got a few problems though that I thought you might be able to help with? >Sometimes I put the wrong information in a list item. Maybe to prevent this I'd be able to modify/update the list item? That's not the only problem though, when there are 50+ items it gets kind of hard to work my way through. Do you think you could maybe add the ability to categorise my items? Obviously, if I have that, I'd also like to be able to view by category!
    Oh and finally, a few of you were really great and did this last time but is there a way you can somehow make my list retain state so that I don't have to re-type it everytime I turn my computer on again?
    The newest To-do list should be capable of the following functionality:
    Modifying an existing list item
    Be able to give a list item a category. The list item should be able to take an arbitrary amount of categorys
    View by category - All list items should be able to be sorted and output by category to make it easier to wade through submissions
    Retain state
    Thanks!</p>
    
    <h2>Formal Inputs &amp; Outputs</h2>
    
    <h3>Output description</h3>
    
    <p>Any output that is created should be user-friendly. When I'm viewing my to-do list, I should be able to easily discern one list item from another.
    Examples</p>
    
    <p>(don't take this too literally, do it how you would like to do it)
    Categorisation</p>
    
    <h3>Input:</h3>
    
    <p>Category Output</p>
    
    <p>Input:
    Output:
        ----PROGRAMMING----
        - A pixel is not a pixel is not a pixel
        - The Scheme Programming Language
        - Memory in C
        - Haskell's School of Music
        - Algorithmic Symphonies from one line of code</p>
    
    <p>----MUSIC----
        - Modes in Folk Music
        - The use of the Melodic Minor Scale
        - Haskell's School of Music
        - Algorithmic Symphonies from one line of code</p>
    
    <p>----MUSIC &amp; PROGRAMMING----
        - Haskell's School of Music
        - Algorithmic Symphonies from one line of code </p>
  </blockquote>
  
  <p>Modifying an item</p>
  
  <blockquote>
    <p>updateItem('Create Sine Waves in C', 'Create Sine Waves in Python');
        //The item has now changed from 'Create Sine Waves in C' to 'Create Sine    Waves in Python'. This should be reflected in the viewList function/method you  have created.</p>
  </blockquote>
  
  <p>The error I'm having is that I get 'Access violation reading location '. That's because in my <code>List&lt;std::string&gt;</code> I realized while debugging that inside any of that list's nodes <code>data = &lt;Error reading characters of string&gt;</code>, but before getting out of <code>ToDoList::addItem()</code> <code>data = "Take a shower"</code>(or whatever data has to be equal to).</p>
  
  <p>I hope I'm explaining my problem correctly.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  
  template&lt;class T&gt;
  struct Node
  {
      T data;
      Node* next = nullptr;
      Node();
      Node(T item);
  };
  
  template&lt;class T&gt;
  Node&lt;T&gt;::Node()
  { }
  
  template&lt;class T&gt;
  Node&lt;T&gt;::Node(T item) : data(item)
  { }
  
  template&lt;class T&gt;
  class List
  {
      Node&lt;std::string&gt;* head;
      Node&lt;std::string&gt;* tail;
      int size;
  
  public:
      List();
      ~List();
  
      void addItem(T item);
      void deleteItem(T item);
      void display() const;
  };
  template&lt;class T&gt;
  List&lt;T&gt;::List() : size(0)
  {
      head = new Node&lt;std::string&gt;();
      tail = head;
  }
  
  template&lt;class T&gt;
  List&lt;T&gt;::~List()
  {
      auto* temp = head;
      for (int i = 0; i &lt; size; ++i){
          head = head-&gt;next;
          delete temp;
  
          temp = head;
      }
  }a
  
  template&lt;class T&gt;
  void List&lt;T&gt;::addItem(T item)
  {
      tail-&gt;data = item;
      tail = tail-&gt;next = new Node&lt;std::string&gt;;
      size++;
  }
  
  template&lt;class T&gt;
  void List&lt;T&gt;::deleteItem(T item)
  {
      auto* temp = head;
      for (int i = 0; i &lt; size; ++i){
          if (temp-&gt;data == item){
              if (temp == head){
                  auto* h2 = head-&gt;next;
                  delete temp;
                  temp = 0;
                  head = h2;
              }
  
              else if (temp == tail){
                  delete temp;
                  temp = 0;
              }
  
              else{
                  temp-&gt;data = temp-&gt;next-&gt;data;
                  auto* te = temp-&gt;next-&gt;next;
                  delete temp-&gt;next;
                  temp-&gt;next = 0;
                  temp-&gt;next = te;
              }
  
              --size;
              break;
          }
  
          temp = temp-&gt;next;
      }
  }
  
  template &lt;class T&gt;
  void List&lt;T&gt;::display() const
  {
      auto* temp = head;
      for (int i = 0; i &lt; size; ++i)
      {
          std::cout &lt;&lt; "- " &lt;&lt; temp-&gt;data;
          temp = temp-&gt;next;
      }
  }
  
  class Category
  {
      std::string name;
      List&lt;std::string&gt; list;
  
  public:
      Category(std::string name);
  
      void addItemToCategory(std::string item);
      void doneItemInCategory(std::string item);
      void displayCategory() const;
  
      std::string getName() const;
  };
  
  Category::Category(std::string _name) : name(_name)
  { }
  
  void Category::addItemToCategory(std::string item)
  {
      list.addItem(item);
  }
  
  void Category::doneItemInCategory(std::string item)
  {
      list.deleteItem(item);
  }
  
  void Category::displayCategory() const
  {
      list.display();
  }
  
  std::string Category::getName() const
  {
      return name; 
  }
  
  class ToDoList 
  {
      std::vector&lt;Category&gt; categories;
  
  public:
      ToDoList();
  
      void addItem(std::string item, std::string category);
  
      void viewList();
  };
  
  ToDoList::ToDoList()
  { }
  
  void ToDoList::addItem(std::string item, std::string category)
  {
      if (!categories.size()){
          Category newCategory(category);
          newCategory.addItemToCategory(item);
  
          categories.push_back(newCategory);
      }
      else{
          for (auto&amp; cate : categories){
              if (cate.getName() == category){
                  cate.addItemToCategory(item);
                  return;
              }
          }
  
          Category newCategory(category);
          newCategory.addItemToCategory(item);
  
          categories.push_back(newCategory);
      }
  }
  
  void ToDoList::viewList()
  {
      for (const auto&amp; cate : categories){
          std::cout &lt;&lt; "------" &lt;&lt; cate.getName() &lt;&lt; "------\n";
          cate.displayCategory();
      }
      std::cout &lt;&lt; "\n" &lt;&lt;std::endl ;
  }
  
  int main()
  {
      ToDoList list;
  
      list.addItem("Take a shower", "x");
      list.addItem("Go to work", "x");
      list.viewList();
  
      list.addItem("Buy a new phone", "y");
      list.viewList();
  
      std::cin.ignore(2);
  }
  </code></pre>