title: Define an error class for C++
body: <p>I've been trying to define an error class in C++. I have three files; <code>main.cpp</code>, <code>my_class.h</code>, <code>my_class.cpp</code>. If I tried to compile with the below code, all, of course, in three separate files, I would get a multiple definition error of <code>my_exception</code>.</p>
  
  <p>Please note, if I move the entire definition and declaration of <code>my_exception</code> into <code>my_class.cpp</code>, everything compiles fine and works correctly. Why is this, and how should I write this so that I could put the definition of the exception into the <code>.h</code> file? Or should I even put an error class definition into a <code>.h</code> file?</p>
  
  <p><code>main.cpp</code>:</p>
  
  <pre><code>#include my_class.h
  int main(){
      my_class m;
      /* stuff */
  }
  </code></pre>
  
  <p><code>my_class.h</code>:</p>
  
  <pre><code>#ifndef MY_CLASS_H
  #define MY_CLASS_H
  
  #include &lt;iostream&gt;  //irrelevant for this
  #include &lt;exception&gt;
  using namespace std;
  
  //taken from http://www.cplusplus.com/doc/tutorial/exceptions/
  class my_exception : public exception{
      virtual const char* what() throw() { return "message";}
  } me_err;
  
  class my_class{
      my_class() };
  };
  
  #endif // MY_CLASS_H
  </code></pre>
  
  <p><code>my_class.cpp</code>:</p>
  
  <pre><code>#include my_class.h
  my_class::my_class(){ 
      throw me_err; 
  }
  </code></pre>