title: C++ C2440 '<function-style-cast>': cannot convert from 'Node<T> *const ' to 'Iterator<const T>'
body: <p>I have been trying to create iterator for my Linked List implementations. But i am getting an error. My implementation is below:</p>
  
  <pre><code>#ifndef LinkedListIterative_h
  #define LinkedListIterative_h
  
  #include &lt;cstdio&gt;
  
  using std::swap;
  
  template &lt;typename T&gt;
  class LinkedList;
  
  template &lt;typename T&gt;
  class Iterator;
  
  template&lt;typename T&gt;
  class Node
  {
      private:
          friend class Iterator&lt;T&gt;;
          friend class LinkedList&lt;T&gt;;        
  
          T data;
          Node *next;
  
          Node() : next(nullptr) {}
          Node(const T&amp; data) : data(data) {}
  };
  
  template&lt;typename T&gt;
  class Iterator
  {
      private:
          Node&lt;T&gt; *node;
  
      public:
          Iterator() : node(nullptr) {}
  
          Iterator(Node&lt;T&gt; *node) : node(node)  {}
  
          Iterator(const Node&lt;T&gt; *node) : node(node)  {}
  
          Iterator(const Iterator&amp; iterator) : node(iterator.node)
          {
  
          }
  
          Iterator&amp; operator= (const Iterator&amp; rhs)
          {
              if( this != &amp;rhs )
              {
                  node = rhs.node;
              }
          }
  
          Iterator&amp; operator++()
          {
              node = node-&gt;next;
  
              return *this;
          }
  
          Iterator&amp; operator+(size_t index)
          {
              while( index-- &gt; 0 &amp;&amp; (node != nullptr) )
              {
                  ++this;
              }
  
              return *this;
          }
  
          Iterator&amp; operator[](size_t index)
          {
              while( index-- &gt; 0 &amp;&amp; (node != nullptr) )
              {
                  ++this;
              }
  
              return *this;
          }
  
          bool operator==(const Iterator &amp;iter)
          {
              return node == iter.node;
          }
  
          bool operator!=(const Iterator &amp;iter)
          {
              return node != iter.node;
          }
  
          T&amp; operator*() { return node-&gt;data; }
  
  };
  
  template &lt;typename T&gt;
  class LinkedList
  {
      private:
          size_t size;
          Node&lt;T&gt; *first;
          Node&lt;T&gt; *last;
  
          Node&lt;T&gt;* createNode(const T &amp;item)
          {
              Node&lt;T&gt; *node = new Node&lt;T&gt;;
  
              node-&gt;data = item;
              node-&gt;next = nullptr;
  
              return node;
          }
  
          LinkedList(const LinkedList&amp; list){}
  
          LinkedList&amp; operator=(const LinkedList&amp; list){}
  
      public:
          typedef Iterator&lt;T&gt; iterator;
          typedef Iterator&lt;const T&gt; const_iterator;
  
          LinkedList() : size(0), first(nullptr), last(nullptr)
          {
  
          }
  
          // Add item at the end of the list
          void add(const T&amp; item)
          {
              Node&lt;T&gt; *node = createNode(item);
  
              if(first == nullptr)
              {
                  first = node;
                  last  = node;
              }
              else
              {
                  last-&gt;next = node;
                  last = node;
              }
  
              ++size;
          }
  
          void add(const T&amp; item,size_t index)
          {
              if( size == 0 )
              {
                  add(item);
              }
              else if( index == 0 &amp;&amp; size &gt; 0 )
              {
                  addToFirst(item);
              }
              else if( index &gt;= size )
              {
                  addToLast(item);
              }
              else
              {
                  Node&lt;T&gt; *prev = first;
                  Node&lt;T&gt; *curr = first;
  
                  int i = 0;
  
                  while( i++ &lt; index  )
                  {
                      prev = curr;
                      curr = curr-&gt;next;
                  }
  
                  Node&lt;T&gt; *node = createNode(item);
  
                  prev-&gt;next = node;
                  node-&gt;next = curr;
  
                  ++size;
              }
          }
  
          void addToFirst(const T&amp; item)
          {
              Node&lt;T&gt; *node = createNode(item);
  
              node-&gt;next = first;
  
              first = node;
  
              if(size == 0)
                  last = node;
  
              ++size;
          }
  
          void addToLast(const T&amp; item)
          {
              Node&lt;T&gt; *node = createNode(item);
  
              last-&gt;next = node;
  
              last = node;
  
              if(size == 0)
                  first = node;
  
              ++size;
          }
  
          void removeFirst()
          {
              if( first == nullptr )
                  return;
  
              Node&lt;T&gt; *temp = first;
  
              first = first-&gt;next;
  
              --size;
  
              delete temp;
  
              if( size == 0 )
                  last = first;
          }
  
          void remove(const size_t index)
          {
              if( size == 0 || index &gt; size - 1 )
                  throw std::out_of_range("Out of range");
  
              if(index == 0)
              {
                  removeFirst();
                  return;
              }
  
              Node&lt;T&gt; *curr = first;
              Node&lt;T&gt; *prev = first;
  
              size_t i(0);
  
              while( i++ &lt; index )
              {
                  prev = curr;
                  curr = curr-&gt;next;
              }
  
              if( curr == last )
              {
                  last = prev;
              }
  
              prev-&gt;next = curr-&gt;next;
  
              delete curr;
  
              --size;
          }
  
          void removeLast()
          {
              if( first == nullptr )
                  return;
  
              Node&lt;T&gt; *curr = first;
              Node&lt;T&gt; *prev = first;
  
              while( curr != last )
              {
                  prev = curr;
                  curr = curr-&gt;next;
              }
  
              prev-&gt;next = nullptr;
  
              delete last;
  
              last = prev;
  
              --size;
  
              if( size == 0 )
                  first = last;
          }
  
          T&amp; getItem(size_t index) const
          {
              if(index &gt; size)
                  throw std::out_of_range("index out of bound!");
  
              Node&lt;T&gt; *curr = first;
  
              size_t i = 0;
  
              while ( i++ != index )
                  curr = curr-&gt;next;
  
              return curr-&gt;data;
          }
  
          size_t length() const
          {
              return size;
          }
  
          iterator begin()
          {
              return iterator(first);
          }
  
          iterator end()
          {
              return iterator(last); //getting error here
          }
  
          const_iterator begin() const
          {
              return const_iterator(first);
          }
  
          const_iterator end() const
          {
              const_iterator(last);
          }
  
          ~LinkedList()
          {
              Node&lt;T&gt; *curr = first;
  
              while( curr != last )
              {
                  Node&lt;T&gt; *temp = curr;
  
                  curr = curr-&gt;next;
  
                  delete temp;
              }
          }           
  };
  
  
  #endif /* LinkedListIterative_h */
  </code></pre>
  
  <p>The error i am getting is:</p>
  
  <pre><code>Error C2440 '&lt;function-style-cast&gt;': cannot convert from 'Node&lt;T&gt; *const ' to 'Iterator&lt;const T&gt;'   
  </code></pre>
  
  <p>The caller of this inside main is:</p>
  
  <pre><code>void printList(const LinkedList&lt;int&gt;&amp; list)
  {
      size_t index = 0;
  
      LinkedList&lt;int&gt;::const_iterator iter = list.begin();
  
      for(; iter != list.end(); ++iter)
      {
          std::cout &lt;&lt; *iter &lt;&lt; " ";
      }
  
      while( index &lt; list.length() )
      {
          std::cout &lt;&lt; list.getItem(index ++) &lt;&lt; " ";
      }
  
      std::cout &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>I cant figure out whats wrong. </p>