Error - invalid use of incomplete type / forward declaration of

94,478

Solution 1

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

Solution 2

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

Share:
94,478

Related videos on Youtube

Remi M
Author by

Remi M

Updated on June 17, 2020

Comments

  • Remi M
    Remi M about 4 years

    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__ */
    
    • Mike Seymour
      Mike Seymour about 12 years
      You shouldn't use reserved names for header guards; it could lead to problems like stackoverflow.com/questions/3345159
    • kakyo
      kakyo
      Here is a solution to my own situation with the same errors in your title but maybe not for you: I had to include the header of that class under complaint in my cpp, if the class's public method was referred to by another class. I also had very complex dependencies and mixed use of forward declarations and includes. I'm writing it here more as a note than a solution since you already had one.
  • Remi M
    Remi M about 12 years
    Thanks for your reply! sorry, I forgot to mention that "Object.hh" is already included in Player.cpp and MapLink.cpp and still getting that error!
  • Remi M
    Remi M about 12 years
    Thanks for helping! It gives the same error.. I don't understand how something that simple is giving me that much trouble!
  • Remi M
    Remi M about 12 years
    There is no reference to each other in Core.hh/Object.hh
  • Remi M
    Remi M about 12 years
    I wanted to get all SFML includes but you're right, including the whole Core.hh is a bad idea! Trying that
  • Remi M
    Remi M about 12 years
    That's much better! Thank u very much mister Eitan :D

Related