title: Template Class – Symbols not found
body: <p>I've seen a few <a href="https://stackoverflow.com/questions/1682844/templates-template-function-not-playing-well-with-classs-template-member-functi">related</a> <a href="https://stackoverflow.com/questions/999358/undefined-symbols-linker-error-with-simple-template-class"> posts</a>, but I can't really make sense of what I need to do to fix a program I'm making for my entry-level C++ class.</p>
  
  <p>My errors are:</p>
  
  <pre><code>Build Final Project of project Final Project with configuration Debug
  
  Ld "build/Debug/Final Project" normal x86_64
  cd "/Users/nick/Dropbox/|Syncs/Xcode/Final Project"
  setenv MACOSX_DEPLOYMENT_TARGET 10.6
  /Developer/usr/bin/g++-4.2 -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk "-        L/Users/nick/Dropbox/|Syncs/Xcode/Final Project/build/Debug" "-F/Users/nick/Dropbox/|Syncs/Xcode/Final Project/build/Debug" -filelist "/Users/nick/Dropbox/|Syncs/Xcode/Final Project/build/Final Project.build/Debug/Final Project.build/Objects-normal/x86_64/Final Project.LinkFileList" -mmacosx-version-min=10.6 -o "/Users/nick/Dropbox/|Syncs/Xcode/Final Project/build/Debug/Final Project"
  
  Undefined symbols:
    "Vector&lt;double&gt;::Vector()", referenced from:
    _main in main.o
    "Vector&lt;double&gt;::length()", referenced from:
    _main in main.o
    "Vector&lt;double&gt;::Vector(double const&amp;, double const&amp;, double const&amp;)", referenced from:
    _main in main.o
    _main in main.o
    "Vector&lt;double&gt;::getx() const", referenced from:
    _main in main.o
    _main in main.o
    "Vector&lt;double&gt;::gety() const", referenced from:
    _main in main.o
    _main in main.o
  ld: symbol(s) not found
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>Here's what I've got:</p>
  
  <pre><code>//main.cpp
  
  #include &lt;iostream&gt;
  #include "Vector.h"
  
  int main () {
  Vector&lt;double&gt; a(1, 2, 3);
  Vector&lt;double&gt; b(2, 4, 4);
  Vector&lt;double&gt; c;
  
  std::cout &lt;&lt; "Length: " &lt;&lt; b.length() &lt;&lt; std::endl;
  std::cout &lt;&lt; b.getx() &lt;&lt; " ,"  &lt;&lt; b.gety() &lt;&lt; std::endl;
  std::cout &lt;&lt; c.getx() &lt;&lt; " , " &lt;&lt; c.gety() &lt;&lt; std::endl;
  return 0;
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>//Vector.h
  
  template &lt;class T&gt;
  class Vector {
  T x, y, z;
  
  public:
  
  //constructors
  Vector();
  Vector(const T&amp; x, const T&amp; y, const T&amp; z);
  Vector(const Vector&amp; u);
  
  //accessors
  T getx() const;
  T gety() const;
  T getz() const;
  
  //mutators
  void setx(const T&amp; x);
  void sety(const T&amp; y);
  void setz(const T&amp; z);
  
  //operations
  void operator-();
  Vector plus(const Vector&amp; v);
  Vector minus(const Vector&amp; v);
  Vector cross(const Vector&amp; v);
  T dot(const Vector&amp; v);
  void times(const T&amp; s);
  T length();
  };
  </code></pre>
  
  <p>and the Vector.cpp (though I've trimmed some code that's somewhat duplicative – accessors &amp; mutators for y and z, for instance)</p>
  
  <pre><code>//Vector.cpp
  #include "Vector.h"
  #include &lt;iostream&gt;
  #include &lt;math.h&gt;
  
  template class Vector&lt;int&gt;;
  template class Vector&lt;double&gt;;
  
  //default constructor
  template &lt;class T&gt;
  Vector&lt;T&gt;::Vector(): x(0), y(0), z(0) {}
  
  
  //constructor
  template &lt;class T&gt;
  Vector&lt;T&gt;::Vector(const T&amp; x, const T&amp; y, const T&amp; z)
  {
  setx(x);
  sety(y);
  setz(z);
  }
  
  //copy constructor
  template &lt;class T&gt;
  Vector&lt;T&gt;::Vector(const Vector&amp; u)
  {
  x = u.getx();
  y = u.gety();
  z = u.getz();
  }
  
  //x accessor
  template &lt;class T&gt;
  T Vector&lt;T&gt;::getx() const
  {
  return x;
  }
  
  //y accessor
  
  //z accessor
  
  //x mutator
  template &lt;class T&gt;
  void Vector&lt;T&gt;::setx(const T&amp; x)
  {
  Vector::x = x;
  }
  
  //y mutator
  
  //z mutator
  
  //negated Vector
  template &lt;class T&gt;
  void Vector&lt;T&gt;::operator-()
  {
  setx(-this-&gt;getx());
  sety(-this-&gt;gety());
  setz(-this-&gt;getz());
  }
  
  //sum
  template &lt;class T&gt;
  Vector&lt;T&gt; Vector&lt;T&gt;::plus(const Vector&amp; v)
  {
  Vector ret((getx() + v.getx()), (gety() + v.gety()), (getz() + v.getz()));
  return ret;
  }
  
  //difference
  template &lt;class T&gt;
  Vector&lt;T&gt; Vector&lt;T&gt;::minus(const Vector&amp; v)
  {
  Vector ret((getx() - v.getx()), (gety() - v.gety()), (getz() - v.getz()));
  return ret;
  }
  
  //cross product
  template &lt;class T&gt;
  Vector&lt;T&gt; Vector&lt;T&gt;::cross(const Vector&amp; v)
  {
  Vector ret;
  ret.setx(gety()*v.getz() - getz()*v.gety());
  ret.sety(getz()*v.getx() - getx()*v.getz());
  ret.setz(getx()*v.gety() - gety()*v.getx());
  return ret;
  
  }
  
  //dot product
  template &lt;class T&gt;
  T Vector&lt;T&gt;::dot(const Vector&amp; v)
  {
  return (getx()*v.getx() + gety()*v.gety() + getz()*v.getz());   
  }
  
  //scalar times Vector
  template &lt;class T&gt;
  void Vector&lt;T&gt;::times(const T&amp; s)
  {
  setx(getx()*s);
  sety(gety()*s);
  setz(getz()*s);
  }
  
  //length of Vector
  template &lt;class T&gt;
  T Vector&lt;T&gt;::length()
  {
  return sqrt((this-&gt;dot(*this)));
  }
  </code></pre>
  
  <p>So, what the hell's going on? Is it because I have a separate header &amp; .cpp file for Vector? How do I get my main function to recognize functions of my Vector class?</p>