Reputation: 436
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
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
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:
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:
Upvotes: 17
Reputation: 66371
Follow the includes:
Object.hh
- __OBJECT_H__
is definedCore.hh
- __CORE_H__
is definedMapLink.hh
- includes Core.hh
, but the content of that file isn't included because of step 2 and the #ifndef
.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