23

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__ */
2
  • 2
    You shouldn't use reserved names for header guards; it could lead to problems like stackoverflow.com/questions/3345159 Commented Jun 27, 2012 at 16:34
  • 1
    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. Commented Dec 20, 2012 at 15:13

3 Answers 3

17

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".
Sign up to request clarification or add additional context in comments.

3 Comments

There is no reference to each other in Core.hh/Object.hh
I wanted to get all SFML includes but you're right, including the whole Core.hh is a bad idea! Trying that
That's much better! Thank u very much mister Eitan :D
3

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

1 Comment

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!
0

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.

1 Comment

Thanks for helping! It gives the same error.. I don't understand how something that simple is giving me that much trouble!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.