Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to include <code>foo.h</code> in <code>bar.h</code>:</p> <pre><code>#pragma once #include "foo.h" class bar { bar(const foo &amp; f); }; </code></pre> <p>If you need a full class definition, which you do unless you're using a pointer (in which case a forward declaration would suffice), you need to include the file where the class is declared.</p> <p><strong>Why does it compile sometimes and sometimes it fails?</strong></p> <p>Depends on the compiler. My guess is <code>foo.h</code> is included in a file that gets compiled before <code>bar.h</code> is reached. So <code>bar.h</code> already knows about <code>foo</code>'s definition. If the build fails, the files that were succesfully compiled are skipped, and possibly <code>foo.h</code> no longer included. Or if you make a change only in <code>x.cpp</code>, the compiler will only build that file and possibly skip the inclusion of <code>foo.h</code>, which was included in a file that is already compiled.</p> <p>I hope I was clear.</p> <p><strong>Why what you already have isn't enough?</strong></p> <p>You might say that</p> <pre><code>//Bar.cpp #include "foo.h" #include "bar.h" // Bar's definitions </code></pre> <p>includes <code>foo.h</code> before <code>bar.h</code>. Indeed, but what if <code>bar.h</code> is included before <code>foo.h</code> in a different place? That's why it is a good idea to include foo.h in bar.h. Any file including bar.h will no longer need to include <code>foo.h</code> (which it must in order to compile), and it makes more sense to include everything you need in the file. This isn't the case with forward declarations, but, again, you're using a complete type, not a pointer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload