Note that there are some explanatory texts on larger screens.

plurals
  1. POCompiler error in declaring template friend class within a template class
    text
    copied!<p>I have been trying to implement my own linked list class for didactic purposes.</p> <p>I specified the "List" class as friend inside the Iterator declaration, but it doesn't seem to compile.</p> <p>These are the interfaces of the 3 classes I've used:</p> <p><strong>Node.h:</strong></p> <pre><code>#define null (Node&lt;T&gt; *) 0 template &lt;class T&gt; class Node { public: T content; Node&lt;T&gt;* next; Node&lt;T&gt;* prev; Node (const T&amp; _content) : content(_content), next(null), prev(null) {} }; </code></pre> <p><strong>Iterator.h:</strong></p> <pre><code>#include "Node.h" template &lt;class T&gt; class Iterator { private: Node&lt;T&gt;* current; Iterator (Node&lt;T&gt; *); public: bool isDone () const; bool hasNext () const; bool hasPrevious () const; void stepForward (); void stepBackwards (); T&amp; currentElement () const; friend class List&lt;T&gt;; }; </code></pre> <p><strong>List.h</strong></p> <pre><code>#include &lt;stdexcept&gt; #include "Iterator.h" template &lt;class T&gt; class List { private: Node&lt;T&gt;* head; Node&lt;T&gt;* tail; unsigned int items; public: List (); List (const List&lt;T&gt;&amp;); List&amp; operator = (const List&lt;T&gt;&amp;); ~List (); bool isEmpty () const { return items == 0; } unsigned int length () const { return items; } void clear (); void add (const T&amp;); T remove (const T&amp;) throw (std::length_error&amp;, std::invalid_argument&amp;); Iterator&lt;T&gt; createStartIterator () const throw (std::length_error&amp;); Iterator&lt;T&gt; createEndIterator () const throw (std::length_error&amp;); }; </code></pre> <p>And this is the test program I've been trying to run:</p> <p><strong>trial.cpp</strong></p> <pre><code>using namespace std; #include &lt;iostream&gt; #include "List/List.cc" int main () { List&lt;int&gt; myList; for (int i = 1; i &lt;= 10; i++) { myList.add(i); } for (Iterator&lt;int&gt; it = myList.createStartIterator(); !it.isDone(); it.stepForward()) { cout &lt;&lt; it.currentElement() &lt;&lt; endl; } return 0; } </code></pre> <p>When I try to compile it, the compiler gives me the following errors:</p> <p><strong>Iterator.h:26: error: ‘List’ is not a template</strong></p> <p><strong>Iterator.h: In instantiation of ‘Iterator’:</strong></p> <p><strong>trial.cpp:18: instantiated from here</strong></p> <p><strong>Iterator.h:12: error: template argument required for ‘struct List’</strong></p> <p><strong>List.cc: In member function ‘Iterator List::createStartIterator() const [with T = int]’:</strong></p> <p><strong>trial.cpp:18: instantiated from here</strong></p> <p><strong>Iterator.h:14: error: ‘Iterator::Iterator(Node*) [with T = int]’ is private</strong></p> <p><strong>List.cc:120: error: within this context</strong></p> <p>Seems like it is not recognizing the friend declaration. Where did I go wrong?</p>
 

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