Remi M
Remi M

Reputation: 436

Error - invalid use of incomplete type / forward declaration of

My problem is pretty common I know but I've been searching and trying every solutions I found and still does not work. So any help would be greatly appreciated! =)

Thanks in advance!

I have this error at compilation :

g++ -ISFML/include -Iclasses/ -W -Wall -Werror   -c -o classes/Object.o classes/Object.cpp
In file included from classes/Core.hh:18:0,
         from classes/Object.hh:4,
         from classes/Object.cpp:1:
classes/MapLink.hh:9:1: error: invalid use of incomplete type ‘struct Object’
classes/MapLink.hh:6:7: error: forward declaration of ‘struct Object’
In file included from classes/Core.hh:19:0,
         from classes/Object.hh:4,
         from classes/Object.cpp:1:
classes/Player.hh:9:1: error: invalid use of incomplete type ‘struct Object’
classes/MapLink.hh:6:7: error: forward declaration of ‘struct Object’
make: *** [classes/Object.o] Error 1

So basically, I've got a main containing (main.cpp)

#include "Core.hh"

int        main(void)
{
  ...
}

Here's the header file containing all my includes (Core.hh)

#ifndef __CORE_HH__
# define __CORE_HH__

#include ...
#include "Object.hh"
#include "MapLink.hh"
#include "Player.hh"

class Core
{
  ...
};

#endif /* __CORE_HH__ */

And then the files that are causing me troubles (Object.hh)

#ifndef __OBJECT_HH__
# define __OBJECT_HH__

#include "Core.hh"

class Object
{
  ...
};

#endif /* __OBJECT_HH__ */

(MapLink.hh)

#ifndef __MAPLINK_H__
# define __MAPLINK_H__

#include "Core.hh"

class Object;

class MapLink : public Object
{
  ...
};

#endif /* __MAPLINK_H__ */

(Player.hh)

#ifndef __PLAYER_H__
# define __PLAYER_H__

#include "Core.hh"

class Object;

class Player : public Object
{
  ...
};

#endif /* __PLAYER_H__ */

Upvotes: 23

Views: 99789

Answers (3)

zapredelom
zapredelom

Reputation: 1029

Compiler must know full interface of a class for inheritance. In this case, the compiler couldn't see your object. It's necessary to include object.hh file in other files

Upvotes: 3

Eitan T
Eitan T

Reputation: 32920

Problem #1:
You must derive only from a fully declared class, otherwise the compiler wouldn't know what to do.
Remove the forward declaration class Object;.

Problem #2:
You have a circular dependency all over:

  • In "Core.hh" you include "Object.hh", "MapLink.hh" and "Player.hh".
  • In "Object.hh", "MapLink.hh" and "Player.hh" you include "Core.hh".

You need to make sure the each class fully includes the class that it inherits from.
I'm not sure how the classes interact with each other, you should provide that detail in the question.
My guess is that you need to modify your inclusions as follows:

  • Modify "MapLink.hh" and "PlayerLink.hh" so that they include "Object.hh", not "Core.hh"
  • Modify "Object.hh" so that it doesn't include "Core.hh".

Upvotes: 17

molbdnilo
molbdnilo

Reputation: 66371

Follow the includes:

  1. Object.hh - __OBJECT_H__ is defined
  2. Core.hh - __CORE_H__ is defined
  3. MapLink.hh - includes Core.hh, but the content of that file isn't included because of step 2 and the #ifndef.
  4. Player.hh - Same as step 3.

So MapLink.hh and Player.hh don't get to see the definition of Object before you try to inherit from it, and you can't inherit from a class that hasn't been fully defined.

To fix: specifically include the header of the class that you're inheriting from.
That is, add #include "Object.hh" to MapLink.hh and Player.hh.

Upvotes: 0

Related Questions