Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Problem with inheriting classes

Credits go to Edgar Reynaldo and Tobias Dammers for helping out!
This thread is locked; no one can reply to it. rss feed Print
Problem with inheriting classes
Desmond Taylor
Member #11,943
May 2010
avatar

Hey guys, I have a problem with inheriting classes and I've read this book I have in front of me for the last half an hour and still cannot get it to work.

I have attached the entire project here since I've only just started.

Any help with this would be very appreciated.

The errors:

#SelectExpand
1||=== Game, Debug ===| 2..\include\graphics.h|7|error: invalid use of incomplete type 'struct Game'| 3..\include\fwd\game.h|4|error: forward declaration of 'struct Game'| 4||=== Build finished: 2 errors, 0 warnings ===|

Tobias Dammers
Member #2,604
August 2002
avatar

It means that you're doing something in graphics.h that requires knowledge of the full definition of struct Game, but you'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).

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Desmond Taylor
Member #11,943
May 2010
avatar

I have it including a forward declaration of the class Game and I get those errors. I've attached all of the source code on the first post to show how I'm doing it to save posting parts of code.

Added a video https://www.facebook.com/video/video.php?v=121471064624660

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

In include/graphics.h

class Graphics : public Game {/*...*/};

You didn't include 'include/game.h' there, so it has no idea how to inherit the Graphics class from the Game class. A forward declaration will not work there.

In include/game.h

class Game
{
    public:
        Game();
        ~Game();

        void Run();
        void Error( const char* format, ... );

        Graphics* gfx;

    private:

};

You didn't include 'include/graphics.h' or 'include/fwd/graphics.h' there, so it has no idea what a Graphics* is.

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.

Desmond Taylor
Member #11,943
May 2010
avatar

I included stdafx.h that includes those :/ I'll give it a shot in a sec :D

Edit: Nope that didn't work either. I think there is a problem with my compiler.

Append: Fixed by creating an Error class named CError and setting classes that need the error functions to work.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Why would anyone think to look in stdafx.h? I thought that was some kind of MSVC thing...

Only include what actually needs to be included, don't just mash everything into a single header and then include that everywhere.

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...

So game.h looks like :

#SelectExpand
1#ifndef __GAME_H_INCLUDED 2#define __GAME_H_INCLUDED 3 4#ifndef __STDAFX_H_INCLUDED 5#define __STDAFX_H_INCLUDED 6 7#include <allegro5/allegro.h> 8#include <allegro5/allegro_native_dialog.h> 9#include <allegro5/allegro_image.h> 10 11#include <stdio.h> 12 13#include "fwd/game.h" 14#include "fwd/graphics.h" 15 16#include "game.h" 17#include "graphics.h" 18 19#endif // __STDAFX_H_INCLUDED 20 21class Game 22{ 23 public: 24 Game(); 25 ~Game(); 26 27 void Run(); 28 void Error( const char* format, ... ); 29 30 Graphics* gfx; 31 32 private: 33 34}; 35 36#endif // __GAME_H_INCLUDED

So game.h tries to include itself, but it'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't been completely declared yet, resulting in an invalid use of incomplete type struct Game.

Something similar happens in graphics.h.

Only include what is completely necessary for each file.

If you need a forward declaration, do it in the header that needs it.

Oscar Giner
Member #2,207
April 2002
avatar

You should only include in stdafx.h standard and 3rd party library headers, not headers from your project.

The point of stdafx.h is that it'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's why you shouldn't include there project headers).

append:

Why would anyone think to look in stdafx.h? I thought that was some kind of MSVC thing...

It's just a standard header as any other in the project. It's just the usual name given to the header that will be used for generating the precompiled header (but you can ignore this, it's just a build optimization, it doesn't change any semantics).

Desmond Taylor
Member #11,943
May 2010
avatar

I got it all working now guys. Thanks for your help :D

I shall defiantly be keeping that in mind from now on :P

Go to: