Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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__ */
like image 781
Remi M Avatar asked Jun 27 '12 12:06

Remi M


2 Answers

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".
like image 76
Eitan T Avatar answered Nov 08 '22 21:11

Eitan T


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

like image 29
zapredelom Avatar answered Nov 08 '22 19:11

zapredelom