title: Templates, nested classes, and "expected constructor, destructor, or conversion before '&' token"
body: <p>While working with some templates and writing myself a basic container class with iterators, I found myself needing to move the body of member functions from a template class into a separate file to conform to style guidelines. However, I've run into an interesting compile error:</p>
  
  <blockquote>
    <p>runtimearray.cpp:17: error: expected
    constructor, destructor, or type
    conversion before '&amp;' token
    runtimearray.cpp:24: error: expected
    constructor, destructor, or type
    conversion before '&amp;' token
    runtimearray.cpp:32: error: expected
    constructor, destructor, or type
    conversion before '&amp;' token
    runtimearray.cpp:39: error: expected
    constructor, destructor, or type
    conversion before '&amp;' token
    runtimearray.cpp:85: error: expected
    constructor, destructor, or type
    conversion before 'RuntimeArray'
    runtimearray.cpp:91: error: expected
    constructor, destructor, or type
    conversion before 'RuntimeArray'</p>
  </blockquote>
  
  <p>runtimearray.h:</p>
  
  <pre><code>#ifndef RUNTIMEARRAY_H_
  #define RUNTIMEARRAY_H_
  
  template&lt;typename T&gt;
  class RuntimeArray
  {
   public:
    class Iterator
    {
      friend class RuntimeArray;
     public:
      Iterator(const Iterator&amp; other);
  
      T&amp; operator*();
      Iterator&amp; operator++();
      Iterator&amp; operator++(int);
      Iterator&amp; operator--();
      Iterator&amp; operator--(int);
      bool operator==(Iterator other);
      bool operator!=(Iterator other);
  
     private:
      Iterator(T* location);
  
      T* value_;
    };
  
    RuntimeArray(int size);
    ~RuntimeArray();
  
    T&amp; operator[](int index);
  
    Iterator Begin();
    Iterator End();
  
   private:
    int size_;
    T* contents_;
  };
  
  #endif  // RUNTIMEARRAY_H_
  </code></pre>
  
  <p>runtimearray.cpp:</p>
  
  <pre><code>#include "runtimearray.h"
  
  template&lt;typename T&gt;
  RuntimeArray&lt;T&gt;::Iterator::Iterator(const Iterator&amp; other)
      : value_(other.value_)
  {
  }
  
  template&lt;typename T&gt;
  T&amp; RuntimeArray&lt;T&gt;::Iterator::operator*()
  {
    return *value_;
  }
  
  template&lt;typename T&gt;
  RuntimeArray&lt;T&gt;::Iterator&amp; RuntimeArray&lt;T&gt;::Iterator::operator++()
  {
    ++value_;
    return *this;
  }
  
  template&lt;typename T&gt;
  RuntimeArray&lt;T&gt;::Iterator&amp; RuntimeArray&lt;T&gt;::Iterator::operator++(int)
  {
    Iterator old = *this;
    ++value_;
    return old;
  }
  
  template&lt;typename T&gt;
  RuntimeArray&lt;T&gt;::Iterator&amp; RuntimeArray&lt;T&gt;::Iterator::operator--()
  {
    --value_;
    return *this;
  }
  
  template&lt;typename T&gt;
  RuntimeArray&lt;T&gt;::Iterator&amp; RuntimeArray&lt;T&gt;::Iterator::operator--(int)
  {
    Iterator old = *this;
    --value_;
    return old;
  }
  
  template&lt;typename T&gt;
  bool RuntimeArray&lt;T&gt;::Iterator::operator==(Iterator other)
  {
    return value_ == other.value_;
  }
  
  template&lt;typename T&gt;
  bool RuntimeArray&lt;T&gt;::Iterator::operator!=(Iterator other)
  {
    return value_ != other.value_;
  }
  
  template&lt;typename T&gt;
  RuntimeArray&lt;T&gt;::Iterator::Iterator(T* location)
      : value_(location)
  {
  }
  
  template&lt;typename T&gt;
  RuntimeArray&lt;T&gt;::RuntimeArray(int size)
      : size_(size),
        contents_(new T[size])
  {
  }
  
  template&lt;typename T&gt;
  RuntimeArray&lt;T&gt;::~RuntimeArray()
  {
    if(contents_)
      delete[] contents_;
  }
  
  template&lt;typename T&gt;
  T&amp; RuntimeArray&lt;T&gt;::operator[](int index)
  {
    return contents_[index];
  }
  
  template&lt;typename T&gt;
  RuntimeArray&lt;T&gt;::Iterator RuntimeArray&lt;T&gt;::Begin()
  {
    return Iterator(contents_);
  }
  
  template&lt;typename T&gt;
  RuntimeArray&lt;T&gt;::Iterator RuntimeArray&lt;T&gt;::End()
  {
    return Iterator(contents_ + size_);
  }
  </code></pre>
  
  <p>How can I make these errors go away? The files make sense to me, but alas, it's the compiler's say that matters.</p>