title: Trying to implement my own vector class, keep getting "undefined reference" error
body: <p>I'm trying to implement my own vector class as an exercise of the book Accelerated C++, but I'm getting these errors and I can't figure out how to fix them:</p>
  
  <p>C:\MinGW\bin..\lib\gcc\mingw32\3.4.2........\include\c++\3.4.2\bits\allocator.h||undefined reference to `vec::create()'|</p>
  
  <p>::~vec()]+0x43):C:\MinGW\bin..\lib\gcc\mingw32\3.4.2........\include\c++\3.4.2\bits\allocator.h||undefined reference to `vec::uncreate()'|</p>
  
  <p>I'm using code::blocks and both files are in the same project, so I'm pretty sure it's not a linking issue.</p>
  
  <p>Here is the vec.h file</p>
  
  <pre><code>#ifndef VEC_H
  #define VEC_H
  
  #include &lt;cstddef&gt;
  #include &lt;memory&gt;
  
  template &lt;class T&gt; class vec{
  public:
      typedef T* iterator;
      typedef const T* const_iterator;
      typedef size_t size_type;
      typedef T value_type;
  
      vec(){
          create();
      }
  
      explicit vec(size_type n, const T&amp; val = T()){
          create(n,val);
      }
  
      vec(const vec&amp; v){
          create(v.begin(), v.end());
      }
  
      ~vec(){
          uncreate();
      }
  
      size_type size() const{
          return avail - data;
      }
  
      T&amp; operator[](size_type i){
          return data[i];
      }
  
      const T&amp; operator[](size_type i) const{
          return data[i];
      }
  
      vec&amp; operator = (const vec&amp;);
  
      void push_back(const T&amp; t){
          if(avail == limit)
              grow();
          unchecked_append(t);
      }
  
      iterator begin(){ return data; }
      const_iterator begin() const{ return data; }
  
      iterator end(){ return avail; }
      const_iterator end() const{ return avail; }
  
  private:
      iterator data; //first element
      iterator avail;//one past the last available element
      iterator limit;//one past the total allocated memory
  
      //facilities for memory allocation
  
      std::allocator&lt;T&gt; alloc; //object to handle memory allocation
  
      //allocate and initialize the underlying array
      void create();
      void create(size_type, const T&amp;);
      void create(const_iterator, const_iterator);
  
      //destroy the elements in the array and free the memory
      void uncreate();
  
      //support functions for push_back
      void grow();
      void unchecked_append(const T&amp;);
  };
  
  #endif // VEC_H
  </code></pre>
  
  <p>and the vec.cpp file</p>
  
  <pre><code>#include "vec.h"
  
  template &lt;class T&gt; vec&lt;T&gt;&amp; vec&lt;T&gt;::operator=(const vec&amp; rhs){
      //check for self-assignment
      if(&amp;rhs != this){
          //free the array in the left-hand side
          uncreate();
  
          //copy elements from the right-hand to the left-hand side
          create(rhs.begin(), rhs.end());
      }
  
      return *this;
  }
  
  template &lt;class T&gt; void vec&lt;T&gt;::create(){
      data = avail = limit = 0;
  }
  
  template &lt;class T&gt; void vec&lt;T&gt;::create(size_type n, const T&amp; val){
      data = alloc.allocate(n);
      limit = avail = data+n;
      uninitialized_fill(data, limit, val);
  }
  
  template &lt;class T&gt; void vec&lt;T&gt;::create(const_iterator i, const_iterator j){
      data = alloc.allocate(j-i);
      limit = avail = uninitialized_copy(i, j, data);
  }
  
  template &lt;class T&gt; void vec&lt;T&gt;::uncreate(){
      if(data){
          //destroy in reverse order the objects that were constructed
          iterator it = avail;
          while(it != data)
              destroy(--it);
  
          //return all the space that was allocated
          alloc.deallocate(data, limit-data);
      }
  
      data = limit = avail = 0;
  }
  
  template &lt;class T&gt; void vec&lt;T&gt;::grow(){
      //when growing, allocate twice as much space as currently in use
      size_type new_size = max(2*(limit-data), ptrdiff_t(1));
  
      //allocate new space and copy existing elements to the new space
      iterator new_data  = alloc.allocate(new_size);
      iterator new_avail = uninitialized_copy(data, avail, new_data);
  
      //return the old space
      uncreate();
  
      //reset pointers to point to the newly allocated space
      data  = new_data;
      avail = new_avail;
      limit = data + new_size;
  }
  
  //assumes avail points to allocated, but uninitialized space
  template &lt;class T&gt; void vec&lt;T&gt;::unchecked_append(const T&amp; val){
      alloc.construct(avail++, val);
  }
  </code></pre>
  
  <p>What am I doing wrong ?</p>