title: Initializing template variable for a linked list
body: <p>I'm creating a linked list class, which uses an internal <code>Link</code> class and a template class to define the types that can be stored in the linked list.</p>
  
  <p>However, in my method <code>popValue</code> which should return template type <code>T</code>, I cannot initialise a default type (in this case, called <code>Point</code>), despite the fact that the type stored in each Link has a default constructor - which according to <a href="https://stackoverflow.com/questions/2143022/how-to-correctly-initialize-variable-of-template-type">these questions</a>, indicates that <code>T retval = T() / T{}</code> should work. </p>
  
  <p>Additionally I dont see how the error message corresponds, given that <code>T retval</code> is not a reference type?</p>
  
  <p>Hence, the method in question:</p>
  
  <pre><code>template&lt;typename T&gt;
  T LinkedList&lt;T&gt;::popValue() {
      T retval = T(); //This doesnt work
      Point test = Point(); //But this is allowed?
      if (head != 0) {
          Link *n = head;
          retval = head-&gt;value;
          head = head-&gt;next;
          delete n;
      }
      return retval;
  }
  </code></pre>
  
  <p>Gives the following error <code>value-initialization of reference type 'Point&amp;'</code>:</p>
  
  <p><a href="https://i.stack.imgur.com/e6eFBm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/e6eFBm.png" alt="enter image description here"></a></p>
  
  <p>How can I initialise this template variable to a default value, so that if there is no elements in the LinkedList, this default value can be returned, given that the methods outlined in other questions don't seem to work? </p>
  
  <p>Thanks very much,</p>
  
  <p>David</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>From comments, I looked at specialisation of the <code>LinkedList.h</code> and found I made the major error of using it as <code>LinkedList&lt;Point&amp;&gt; list;</code>.
  When used in template form, this means that <code>T</code> is trying to instantiate a reference type - which is not allowed.</p>
  
  <p>For reference, I have included a concise code listing of what I thought would be the relevant code (but have removed non-significant includes, guard and namespaces for brevity)...:</p>
  
  <p><strong>LinkedList.h</strong></p>
  
  <pre><code>template&lt;typename T&gt;
      class LinkedList {
      public:
          struct Link { ///&lt; Struct inside the class LinkedList
              Link(T val): value(val), next(0){};
              T value;
              Link *next;
              ~Link() {
                  delete next;
              } //Iteratively delete
          };
          T popValue(); ///&lt; returns first element and deletes Link.
      private:
          Link *head; // this is the private member variable. It is just a pointer to the first Node
      };
  
  #include "LinkedList.cpp" //Allows definition of template methods in .cpp file
  </code></pre>
  
  <p><strong>LinkedList.cpp (erroneous method only)</strong></p>
  
  <pre><code>    template&lt;typename T&gt;
      T LinkedList&lt;T&gt;::popValue() {
          T retval = T(); //This doesnt work
          Point test = Point(); // But this does
          if (head != 0) {
              Link *n = head;
              retval = head-&gt;value;
              head = head-&gt;next;
              delete n;
          }
          return retval;
      }
  </code></pre>