title: C++ error: multiple definition of a member function specialized in template class, but I really defined it only once
body: <p>The problem comes from a computer graphics C++ project in which I want to calculate gradient of both scale field and 3D-vector field. We know the gradient of them are different: scale field has 3D-vector gradient while 3D-vector field has a 3x3 matrix gradient. Since all other codes are the same, I am using template to reuse code. But I met with a problem in specializing member functions which has different code for calculating gradient of different data type. The minimized code is as follows:</p>
  
  <pre><code>//======== Main.cpp ======== 
  #include "Render.h"
  int main() {}
  
  //======== Render.cpp ======== 
  #include "Render.h"
  
  //======== Render.h ======== 
  #ifndef __RENDER_H__
  #define __RENDER_H__
  #include "VolumeGrid.h"
  #endif
  
  //======== VolumeGrid.h ======== 
  #ifndef __VOLUMEGRID_H__
  #define __VOLUMEGRID_H__
  
  #include "Volume.h"
  
  template &lt; typename U &gt;
  class _Grid {
  public:
      const typename GradType&lt;U&gt;::GType grad(const Vector&amp; x) const;
      U * values = nullptr;
  };
  
  template &lt;&gt;
  const Vector _Grid&lt;float&gt;::grad(const Vector&amp; x) const {
      return Vector();
  }
  
  template &lt;&gt;
  const Matrix _Grid&lt;Vector&gt;::grad(const Vector&amp; x) const {
      return Matrix();
  }
  
  #endif
  
  //======== Volumn.h ========
  #ifndef __VOLUME_H__
  #define __VOLUME_H__
  
  #include "Vector.h"
  #include "Matrix.h"
  
  template &lt;typename U&gt;
  struct GradType {
     typedef int GType;
  };
  
  template&lt;&gt;
  struct GradType&lt;float&gt; {
     typedef Vector GType;
  };
  
  template&lt;&gt;
  struct GradType&lt;Vector&gt; {
     typedef Matrix GType;
  };
  
  template&lt; typename U &gt;
  class Volume {
  public:
     typedef U volumeDataType;
     typedef typename GradType&lt;U&gt;::GType volumeGradType;
  };
  
  #endif
  
  
  //======== Vector.h ======== 
  #ifndef __VECTOR_H__
  #define __VECTOR_H__
  
  class Vector {
  public:
      float xyz[3] = { 0,0,0 };
  };
  
  #endif
  
  //======== Matrix ========
  #ifndef __MATRIX_H__
  #define __MATRIX_H__
  
  class Matrix {
    public:
        float m[3][3];
  };
  
  #endif
  </code></pre>
  
  <p>The error message is:</p>
  
  <pre><code>build/Debug/GNU-Linux/Render.o: In function `Vector::Vector()':
  /home/CppApplication_1/VolumeGrid.h:19:
  multiple definition of `_Grid&lt;float&gt;::grad(Vector const&amp;) const'
  build/Debug/GNU-Linux/Main.o:/home/CppApplication_1/VolumeGrid.h:19:
  first defined here
  build/Debug/GNU-Linux/Render.o: In function
  `_Grid&lt;Vector&gt;::grad(Vector const&amp;) const':
  /home/CppApplication_1/VolumeGrid.h:24:
  multiple definition of `_Grid&lt;Vector&gt;::grad(Vector const&amp;) const'
  build/Debug/GNU-Linux/Main.o:/home/CppApplication_1/VolumeGrid.h:24:
  first defined here
  </code></pre>
  
  <p>As you can see from the code, the two specialized <code>grad</code> function corresponding to different data types are defined only once in VolumeGrid.h, as member function of class <code>Grid&lt;float&gt;</code> and <code>Grid&lt;Vector&gt;</code>, respectively. But the error message says there are multiple definitions of them. The code is compiled with g++ 4.8.4 with C++11 enabled on ubuntu 14.04 64-bit (it compiles well on Visual Studio 2015). The code above is minimized in that removal of any line, e.g., <code>#include "Render.h"</code> in Main.cpp, will make the error disappear. The header inclusion structure and class inheritance hierarchy should not be changed because they are used in actual project. So could you please tell me where is the problem in specializing the <code>grad</code> function and how to fix it? Thanks a lot for your help.</p>