title: Basic ODR violation: member functions in .h files
body: <p>Disclaimer: This is probably a basic question, but I'm a theoretical physicist by training trying to learn to code properly, so please bear with me.</p>
  <p>Let's say that I want to model a fairly involved physical system. In my understanding, one way of modelling this system is to introduce it as a class. However, since the system involved, the class will be large, with potentially many data members, member functions and subclasses. Having the main program and this class in one file will be very cluttered, so to give a better overview of the project I tend to put the class in a separate .h file. Such that I'd have something like:</p>
  <pre><code>//main.cpp
  
  #include &quot;tmp.h&quot;
  
  int main()
  {
      myclass aclass;
  
      aclass.myfunction();
  
      return 0;
  }
  
  </code></pre>
  <p>and</p>
  <pre><code>// tmp.h
  
  class myclass
  {
      // data members
      double foo;
      double bar;
  
      public: 
  
      // function members
      double myfunction();
  };
  
  double myclass::myfunction()
  {
      return foo + bar;
  }
  </code></pre>
  <p>This however, amounts to the following compiler warning in my new compiler: <code>function definitions in header files can lead to ODR violations</code>. My question then is this: what is actually the preferred way of dealing with a situation like this? I guess I can just make tmp.h into tmp.cpp, but to the best of my understanding, this is the intended use of .h files?</p>