Note that there are some explanatory texts on larger screens.

plurals
  1. POMissing Template Arguments
    text
    copied!<p>This is part of a homework assignment, but I've reduced the problem as far as possible. The code presented doesn't really do anything, just create some objects, so I am down to pure language issues.</p> <p>I think my question is: Is it possible for a template class to create another class of the same template type?</p> <p>The assignment is to have a main.cc as following:</p> <pre class="lang-cpp prettyprint-override"><code>#include "linkedlist.hh" int main() { LinkedList&lt;int&gt; aList; ListIterator elements = aList.iterator(); // if commented out, then no error } </code></pre> <p>I have the following for linkedlist.hh:</p> <pre class="lang-cpp prettyprint-override"><code>template &lt;typename T&gt; class LinkedList; template &lt;typename T&gt; class ListIterator; template &lt;typename T&gt; class LinkedList { public: ListIterator&lt;T&gt; iterator(); }; template &lt;typename T&gt; ListIterator&lt;T&gt; LinkedList&lt;T&gt;::iterator() { return new ListIterator&lt;T&gt;; } template &lt;typename T&gt; class ListIterator { }; </code></pre> <p>Which give the following error with g++-4.6:</p> <pre><code>main.cc: In function ‘int main()’: main.cc:6:18: error: missing template arguments before ‘elements’ main.cc:6:18: error: expected ‘;’ before ‘elements’ </code></pre> <p>And a similar error with clang++-2.9:</p> <pre><code>main.cc:6:5: error: cannot refer to class template 'ListIterator' without a template argument list ListIterator elements = list.iterator(); ^ In file included from main.cc:1: ./linkedlist.hh:16:11: note: template is declared here class ListIterator { </code></pre> <hr> <h2>UPDATE: Yes if I could change main.cc, then I could do the following, but I don't think that is what the assignment is.</h2> <p>main.cc</p> <pre class="lang-cpp prettyprint-override"><code>#include "linkedlist.hh" int main() { LinkedList&lt;int&gt; aList; ListIterator&lt;int&gt; elements = aList.iterator(); } </code></pre> <p>and linkedlist.hh</p> <pre class="lang-cpp prettyprint-override"><code>template &lt;typename T&gt; class LinkedList; template &lt;typename T&gt; class ListIterator; template &lt;typename T&gt; class LinkedList { public: ListIterator&lt;T&gt; iterator(); }; template &lt;typename T&gt; ListIterator&lt;T&gt; LinkedList&lt;T&gt;::iterator() { ListIterator&lt;T&gt; anIterator; return anIterator; } template &lt;typename T&gt; class ListIterator { }; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload