<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Problem with inheriting classes</title>
		<link>http://www.allegro.cc/forums/view/608489</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sun, 02 Oct 2011 02:13:47 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hey guys, I have a problem with inheriting classes and I&#39;ve read this book I have in front of me for the last half an hour and still cannot get it to work.</p><p>I have attached the entire project <a href="http://www.allegro.cc/files/attachment/604897">here</a> since I&#39;ve only just started.</p><p>Any help with this would be very appreciated.</p><p>The errors:
</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number"> 1</span><span class="k3">|</span><span class="k3">|</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span> Game, Debug <span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">|</span>
<span class="number"> 2</span>..\include\graphics.h<span class="k3">|</span><span class="n">7</span><span class="k3">|</span>error: invalid use of incomplete type <span class="s">'struct Game'</span><span class="k3">|</span>
<span class="number"> 3</span>..\include\fwd\game.h<span class="k3">|</span><span class="n">4</span><span class="k3">|</span>error: forward declaration of <span class="s">'struct Game'</span><span class="k3">|</span>
<span class="number"> 4</span><span class="k3">|</span><span class="k3">|</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span> Build finished: <span class="n">2</span> errors, <span class="n">0</span> warnings <span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">|</span>
</div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Desmond Taylor)</author>
		<pubDate>Sat, 01 Oct 2011 14:02:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It means that you&#39;re doing something in graphics.h that requires knowledge of the full definition of <tt>struct Game</tt>, but you&#39;re only including the forward definition. Typical examples are passing an instance by value, or accessing a member. Either include the full definition, or better yet, move those parts that need the internals into grahpics.c (if this is possible).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Sat, 01 Oct 2011 16:22:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I have it including a forward declaration of the class Game and I get those errors. I&#39;ve attached all of the source code on the first post to show how I&#39;m doing it to save posting parts of code.</p><p>Added a video <a href="https://www.facebook.com/video/video.php?v=121471064624660">https://www.facebook.com/video/video.php?v=121471064624660</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Desmond Taylor)</author>
		<pubDate>Sat, 01 Oct 2011 16:31:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>In include/graphics.h
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">class</span> Graphics <span class="k2">:</span> <span class="k1">public</span> Game <span class="k2">{</span><span class="c">/*...*/</span><span class="k2">}</span><span class="k2">;</span>
</pre></div></div><p>
You didn&#39;t include &#39;include/game.h&#39; there, so it has no idea how to inherit the Graphics class from the Game class. A forward declaration will not work there.</p><p>In include/game.h
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">class</span> Game
<span class="k2">{</span>
    public:
        Game<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
        ~Game<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>

        <span class="k1">void</span> Run<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
        <span class="k1">void</span> Error<span class="k2">(</span> <span class="k1">const</span> <span class="k1">char</span><span class="k3">*</span> format, ... <span class="k2">)</span><span class="k2">;</span>

        Graphics<span class="k3">*</span> gfx<span class="k2">;</span>

    private:

<span class="k2">}</span><span class="k2">;</span>
</pre></div></div><p>
You didn&#39;t include &#39;include/graphics.h&#39; or &#39;include/fwd/graphics.h&#39; there, so it has no idea what a Graphics* is.</p><p>As an aside, making separate headers for forward declarations is silly. If you need a forward declaration in some other header, just write it there.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Sat, 01 Oct 2011 20:44:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I included stdafx.h that includes those :/ I&#39;ll give it a shot in a sec <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" /></p><p>Edit: Nope that didn&#39;t work either. I think there is a problem with my compiler.</p><p>Append: Fixed by creating an Error class named CError and setting classes that need the error functions to work.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Desmond Taylor)</author>
		<pubDate>Sat, 01 Oct 2011 21:19:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Why would anyone think to look in stdafx.h? I thought that was some kind of MSVC thing...</p><p>Only include what actually needs to be included, don&#39;t just mash everything into a single header and then include that everywhere.</p><p>I see the problem now. stdafx.h includes both include/game.h and include/graphics.h, both of which include stdafx.h. Circular inclusion bad...</p><p>So game.h looks like :
</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="p">#ifndef __GAME_H_INCLUDED</span>
<span class="number">  2</span><span class="p">#define __GAME_H_INCLUDED</span>
<span class="number">  3</span>
<span class="number">  4</span><span class="p">#ifndef __STDAFX_H_INCLUDED</span>
<span class="number">  5</span><span class="p">#define __STDAFX_H_INCLUDED</span>
<span class="number">  6</span>
<span class="number">  7</span><span class="p">#include &lt;allegro5/allegro.h&gt;</span>
<span class="number">  8</span><span class="p">#include &lt;allegro5/allegro_native_dialog.h&gt;</span>
<span class="number">  9</span><span class="p">#include &lt;allegro5/allegro_image.h&gt;</span>
<span class="number"> 10</span>
<span class="number"> 11</span><span class="p">#include &lt;stdio.h&gt;</span>
<span class="number"> 12</span>
<span class="number"> 13</span><span class="p">#include "fwd/game.h"</span>
<span class="number"> 14</span><span class="p">#include "fwd/graphics.h"</span>
<span class="number"> 15</span>
<span class="number"> 16</span><span class="p">#include "game.h"</span>
<span class="number"> 17</span><span class="p">#include "graphics.h"</span>
<span class="number"> 18</span>
<span class="number"> 19</span><span class="p">#endif // __STDAFX_H_INCLUDED</span>
<span class="number"> 20</span>
<span class="number"> 21</span><span class="k1">class</span> Game
<span class="number"> 22</span><span class="k2">{</span>
<span class="number"> 23</span>    public:
<span class="number"> 24</span>        Game<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 25</span>        ~Game<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 26</span>
<span class="number"> 27</span>        <span class="k1">void</span> Run<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 28</span>        <span class="k1">void</span> Error<span class="k2">(</span> <span class="k1">const</span> <span class="k1">char</span><span class="k3">*</span> format, ... <span class="k2">)</span><span class="k2">;</span>
<span class="number"> 29</span>
<span class="number"> 30</span>        Graphics<span class="k3">*</span> gfx<span class="k2">;</span>
<span class="number"> 31</span>
<span class="number"> 32</span>    private:
<span class="number"> 33</span>
<span class="number"> 34</span><span class="k2">}</span><span class="k2">;</span>
<span class="number"> 35</span>
<span class="number"> 36</span><span class="p">#endif // __GAME_H_INCLUDED</span>
</div></div><p>
So game.h tries to include itself, but it&#39;s header guards prevent it from actually being included. The next thing that happens is that include/graphics.h is included, but class Game hasn&#39;t been completely declared yet, resulting in an invalid use of incomplete type struct Game.</p><p>Something similar happens in graphics.h.</p><p>Only include what is completely necessary for each file.</p><p>If you need a forward declaration, do it in the header that needs it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Sun, 02 Oct 2011 00:41:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You should only include in stdafx.h standard and 3rd party library headers, not headers from your project.</p><p>The point of stdafx.h is that it&#39;s used to generate precompiled headers, that is, the compiler will only parse everything inside stdafx.h the first time you build your project (and after that, only when one of those files changes). That extremely increases build times, as long as nothing inside stdafx.h changes (that&#39;s why you shouldn&#39;t include there project headers).</p><p>append:
</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/608489/932845#target">Edgar Reynaldo</a> said:</div><div class="quote"><p>
Why would anyone think to look in stdafx.h? I thought that was some kind of MSVC thing...
</p></div></div><p>
It&#39;s just a standard header as any other in the project. It&#39;s just the usual name given to the header that will be used for generating the precompiled header (but you can ignore this, it&#39;s just a build optimization, it doesn&#39;t change any semantics).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Oscar Giner)</author>
		<pubDate>Sun, 02 Oct 2011 01:20:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I got it all working now guys. Thanks for your help <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" /></p><p>I shall defiantly be keeping that in mind from now on <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Desmond Taylor)</author>
		<pubDate>Sun, 02 Oct 2011 02:13:47 +0000</pubDate>
	</item>
</rss>
