title: C++ Error: Multiple definitions of class functions
body: <p>I have a C++ classe in a .h file like this:</p>
  
  <pre><code>#ifndef __GLWidget_h__
  #define __GLWidget_h__
  
  class PivotShape
  {
      // This is allowed
      void do_something() { std::cout &lt;&lt; "Doing something\n"; }
  
      // This is not allowed
      void do_something_else();
  }
  
  // This is not allowed
  void PivotShape::do_something_else()
  {
      std::cout &lt;&lt; "Doing something else\n";
  }
  
  #endif
  </code></pre>
  
  <p>If I add methods inside the class declaration everything seems fine. But if I add methods outside of the class declaration, I get errors like this:</p>
  
  <pre><code>/usr/share/qt4/bin/moc GLWidget.h &gt; GLWidget_moc.cpp
  /programs/gcc-4.6.3/installation/bin/g++ -W -Wall -g -c -I./ -I/usr/include/qt4 GLWidget_moc.cpp
  /programs/gcc-4.6.3/installation/bin/g++ main.o GLState.o GLWidget.o MainWindow_moc.o GLWidget_moc.o -L/usr/lib/x86_64-linux-gnu -lQtGui -lQtOpenGL -lQtCore -lGLU -lGL -lm -ldl -o main
  GLWidget.o: In function `std::iterator_traits&lt;float const*&gt;::iterator_category std::__iterator_category&lt;float const*&gt;(float const* const&amp;)':
  /home/&lt;user&gt;/&lt;dir&gt;/&lt;dir&gt;/&lt;dir&gt;/&lt;dir&gt;/&lt;dir&gt;/GLWidget.h:141: multiple definition of `PivotShape::do_someting_else()'
  main.o:/home/&lt;user&gt;/&lt;dir&gt;/&lt;dir&gt;/&lt;dir&gt;/&lt;dir&gt;/&lt;dir&gt;/GLWidget.h:141: first defined here
  </code></pre>
  
  <p>I think the duplication is being caused by this snippet in the Make file. I think the .h files are being converted to _moc.cpp files and this is allowing the multiple inclusions:</p>
  
  <pre><code># Define linker
  LINKER        = /programs/gcc-4.6.3/installation/bin/g++
  
  MOCSRCS       = $(QTHEADERS:.h=_moc.cpp)
  
  # Define all object files to be the same as CPPSRCS but with all the .cpp
  # suffixes replaced with .o
  OBJ           = $(CPPSRCS:.cpp=.o) $(MOCSRCS:.cpp=.o)
  </code></pre>
  
  <p>Is this the problem? If so, how can I fix it? If not, what's going on?</p>
  
  <p>I thought it was illegal in C++ to include class methods inside the body of the class declaration. If this is legal, then it seems like an easy way to solve the problem. Is this legal?</p>
  
  <p>Edit:</p>
  
  <p>I forgot to mention that I had already discovered that declaring the methods as <code>inline</code> works, but I was wondering how to avoid the duplication.</p>