title: Returning an iterator to graph node with function templates
body: <p>I have 2 files, one is a .h file which contains my declarations and another is a .tem file which contains the implementation of my .h file. I'm having issues with the iterator for begin() and end() for my graph node iterator (shown below in "// Iterators for Graph nodes"), which is supposed to return the iterator pointing at the begin or end of an iterated graph.</p>
  
  <p>The following is my code in the Graph.h:</p>
  
  <pre><code>#ifndef _Graph_h
  #define _Graph_h
  
  #include &lt;vector&gt;
  #include &lt;algorithm&gt;
  #include &lt;string&gt;
  #include &lt;memory&gt;
  #include &lt;iostream&gt;
  #include &lt;exception&gt;
  #include &lt;map&gt;
  #include &lt;set&gt;
  #include &lt;typeinfo&gt;
  
  
  namespace gdwg {
  
      template &lt;typename N, typename E&gt; class Graph; // function prototype for Graph class
  
  //-----------------------------------------------------------
  // Iterators for Graph with Nodes N and Edges E
  //-----------------------------------------------------------
  
      // Iterator class for Node N
      template &lt;typename N, typename E&gt; class Node_Iterator {
  
      ******* Some code for public and private members of Node_Iterator
  
      };
  
      // Iterator class for Edge E
      template &lt;typename N, typename E&gt; class Edge_Iterator {
  
      ******* Some code for public and private members of Edge_Iterator
  
      };
  
  template &lt;typename N, typename E&gt; class Graph {
  
      private:
          struct Node;
          struct Edge;
  
          struct Node {
              N val_;
              int numEdges_;
              std::set&lt;std::shared_ptr&lt;Edge&gt;&gt; edges_;
              Node() {}
              Node(const N x) : val_{x} { numEdges_=0; }
              void printNode(N n);
              ~Node();
              void update();
          };
  
          struct Edge {
              std::weak_ptr&lt;Node&gt; orig;
              std::weak_ptr&lt;Node&gt; dest;
              E val_;
              Edge(std::shared_ptr&lt;Node&gt; o, std::shared_ptr&lt;Node&gt; d, E x);
              Edge() {};
              void printEdge();
              ~Edge();
          };
  
      public:
  
          friend class Node_Iterator&lt;N, E&gt;;
          friend class Edge_Iterator&lt;N, E&gt;;
  
  
          ******* Some code for public members of Graph
  
          // Iterators for Graph nodes
          Node_Iterator&lt;N, E&gt; begin() const;
  
          Node_Iterator&lt;N, E&gt; end() const;
  
  
      private:
          std::map&lt; N, std::shared_ptr&lt;Node&gt; &gt; nodes_;
  
  
  };
  
      #include "Graph.tem" // definition and implementation of Node_Iterator, Edge_Iterator and Graph classes
  
  }
  
  #endif
  </code></pre>
  
  <p>This is the definition in the .tem file for the iterators:</p>
  
  <pre><code>template &lt;typename N, typename E&gt;
  Graph&lt;N, E&gt;::Node_Iterator&lt;N, E&gt; Graph&lt;N, E&gt;::begin() const { 
      return Node_Iterator&lt;N, E&gt;(&amp;Graph&lt;N, E&gt;::nodes_);
  }
  
  template &lt;typename N, typename E&gt;
  Graph&lt;N, E&gt;::Node_Iterator&lt;N, E&gt; Graph&lt;N, E&gt;::end() const { 
      return Node_Iterator&lt;N, E&gt;(nullptr);
  }
  </code></pre>
  
  <p>When I tried to compile it, there was the following error code (I will just put the error code for begin() coz the end() error is similar):</p>
  
  <pre><code>tests/Graph.tem:336:14: error: non-template ‘Node_Iterator’ used as template
   Graph&lt;N, E&gt;::Node_Iterator&lt;N, E&gt; Graph&lt;N, E&gt;::begin() const {
                ^~~~~~~~~~~~~
  tests/Graph.tem:336:14: note: use ‘gdwg::Graph&lt;N, E&gt;::template Node_Iterator’ to indicate that it is a template
  tests/Graph.tem:336:1: error: need ‘typename’ before ‘gdwg::Graph&lt;N, E&gt;::Node_Iterator’ because ‘gdwg::Graph&lt;N, E&gt;’ is a dependent scope
   Graph&lt;N, E&gt;::Node_Iterator&lt;N, E&gt; Graph&lt;N, E&gt;::begin() const {
   ^~~~~~~~~~~
  </code></pre>
  
  <p>So I did what it said and added "typename" in front of Graph and also made it a template. But it came up with this error instead:</p>
  
  <pre><code>tests/Graph.tem:336:52: error: prototype for ‘typename gdwg::Graph&lt;N, E&gt;::Node_Iterator&lt;N, E&gt; gdwg::Graph&lt;N, E&gt;::begin() const’ does not match any in class ‘gdwg::Graph&lt;N, E&gt;’
   typename Graph&lt;N, E&gt;::template Node_Iterator&lt;N, E&gt; Graph&lt;N, E&gt;::begin() const {
                                                      ^~~~~~~~~~~
  In file included from tests/test1.cpp:3:0:
  tests/Graph.h:322:23: error: candidate is: gdwg::Node_Iterator&lt;N, E&gt; gdwg::Graph&lt;N, E&gt;::begin() const
     Node_Iterator&lt;N, E&gt; begin() const;
                         ^~~~~
  </code></pre>
  
  <p>So I removed the Graph in front of begin() as I thought it was the problem, but got another error instead:</p>
  
  <pre><code>tests/Graph.tem:336:60: error: non-member function ‘typename gdwg::Graph&lt;N, E&gt;::Node_Iterator&lt;N, E&gt; gdwg::begin()’ cannot have cv-qualifier
   typename Graph&lt;N, E&gt;::template Node_Iterator&lt;N, E&gt; begin() const {
                                                              ^~~~~
  </code></pre>
  
  <p>Can someone tell me what I'm doing wrong?</p>