Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are declaring an instance of <code>Person</code> in your vehicle header file, therefore the compiler needs the full declaration. If you were using a pointer or reference to a person, you could simply forward declare it with <code>class Person;</code>.</p> <p>EDIT: Also, take out that <code>using namespace std;</code> and prefix your variables with <code>std::</code>. It'll save a lot of ball-ache in the future.</p> <p>EDIT2: You also need to include <code>&lt;string&gt;</code> in your header file.</p> <p>Ok, here goes, I'm going to try and keep it nice and simple.</p> <p>When your compiler processes your implementation files (.cpp) it includes the headers you have specified (in this case, that would be <code>vehicle.h</code> and <code>person.h</code>). For every implementation file, the compiler needs to know about every single type that you use so that it can generate the correct code.</p> <p>When it processes an include file, it <strong>still needs to know everything</strong>. So in your <code>vehicle.h</code> header file, you are using <code>Person</code>. At the point that the compiler hits that, it needs to know how to construct person. Your <code>vehicle.cpp</code> includes <code>person.h</code> before <code>vehicle.h</code> so, no problem. <strong>FOR <code>vehicle.cpp</code></strong>. Anything else that includes <code>vehicle.h</code> but does not include <code>person.h</code>, will give you compiler errors.</p> <p>So when can you get away with a forward declaration and user a pointer or reference?</p> <p>Declaring a pointer, or a reference does not require telling the compiler anything about that class or structure in the header file. You're merely telling the compiler that you have an intention to do so. Provided that the class is forward declared thusly:</p> <pre><code>class Person; </code></pre> <p>Then the compiler says "okee dokee, I'll take that." Then you include the relevant file in your implementation, the compiler sees what you mean and everybody walks home from the bar happy.</p> <p>What has happened in your case, I think, is that the implementation file for vehicle looks good on paper, but something else includes <code>vehicle.h</code> and there is no clue about what a <code>Person</code> is.</p> <p><strong>Your solution</strong></p> <p>Either include <code>person.h</code> in <code>vehicle.h</code> if you must, otherwise change that constructor to take a reference to person (and the member), and forward declare <code>Person</code>. However, not knowing what your program is doing I can't say that even passing-by-reference is correct.</p> <p>And please, remove <code>using namespace std;</code> and change your <code>string</code>'s to <code>std::string</code> :)</p>
 

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