title: File inclusion errors (C++): undefined reference to ______
body: <p>Whenever I compile something that #includes a user-defined class, I get these compilation errors that always look like: <code>main.cpp: undefined reference to Complex::Complex(double, double)</code></p>
  
  <p>I've reduced the problem to a set of three extremely bare files: main.cpp, and for example, Complex.h and Complex.cpp. I still get undefined reference errors. I'm developing in Code::Blocks on Windows but I get the same thing using g++ in Ubuntu. Why does this happen? I've tried building Complex.cpp before main.cpp in Code::Blocks, and I've tried <code>g++ main.cpp Complex.cpp</code> as much as I've tried just <code>g++ main.cpp</code>. Same errors every time.</p>
  
  <pre><code>/*======== main.cpp ========*/
  #include "Complex.h"
  
  int main()
  {
      Complex A(1.0, 1.0);
      return 0;
  }
  
  /*======== Complex.h ========*/
  #ifndef _COMPLEX_H
  #define _COMPLEX_H
  
  class Complex
  {
      public:
      double x, y;
      Complex(double real, double imag);
  };
  
  #endif
  
  /*======== Complex.cpp ========*/
  #include "Complex.h"
  
  Complex::Complex(double real, double imag)
  {
      x = real;
      y = imag;
  }
  </code></pre>
  
  <p>ed: now I get different errors so I must be doing something completely wrong. Using the same code as above, I get:</p>
  
  <pre><code>main.cpp: in function 'int main()':
  main.cpp:5:5: error: 'Complex' was not declared in this scope
  main.cpp:5:13: error: expected ';' before 'A'
  </code></pre>
  
  <p>This is bizarre. Everything worked earlier when I had the class in a .cpp file, but that's "bad practice" so I moved my class definitions into .h files and kept the implementation in .cpp files, and now nothing works.</p>