title: C++ - how to forward declare a class in the same file as main() - getting variable uses undefined class error
body: <p>I'm aware of using function prototypes, and I was under the impression that forward class declarations could serve a similar purpose when <code>main()</code> and a class are in the same file.  For example, I would have expected this would compile:</p>
  
  <pre><code>// main.cpp
  
  #include &lt;iostream&gt;
  
  // class prototypes
  class MyClass;
  
  int main(void)
  {
    MyClass myClass;
  
    // do stuff with myClass here
  
    return(0);
  }
  
  class MyClass
  {
  public:
    int someInt;
    double someDouble;
  
    // more stuff here . . .
  
  };
  </code></pre>
  
  <p>But on the <code>MyClass myClass;</code> line I'm getting the error <code>'myClass' uses undefined class 'MyClass'</code>.  What am I doing wrong?</p>
  
  <p>P.S. I'm aware that I could cut/paste <code>main()</code> below all the classes it uses and that would fix the error, but I'd prefer to keep main() as the first function or class.</p>
  
  <p>P.P.S. I'm aware that in any substantial size production program <code>main()</code>, <code>.h</code> content, and <code>.cpp</code> content would be in 3 separate files.  In this case I'm attempting to write a small example or test program where main and a class(es) are in the same file.</p>