C++ 错误 - 不完整类型的无效使用/前向声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11226561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error - invalid use of incomplete type / forward declaration of
提问by Remi M
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)
所以基本上,我有一个主要的包含(main.cpp)
#include "Core.hh"
int main(void)
{
...
}
Here's the header file containing all my includes (Core.hh)
这是包含我所有包含的头文件 (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)
然后是给我带来麻烦的文件(Object.hh)
#ifndef __OBJECT_HH__
# define __OBJECT_HH__
#include "Core.hh"
class Object
{
...
};
#endif /* __OBJECT_HH__ */
(MapLink.hh)
(地图链接.hh)
#ifndef __MAPLINK_H__
# define __MAPLINK_H__
#include "Core.hh"
class Object;
class MapLink : public Object
{
...
};
#endif /* __MAPLINK_H__ */
(Player.hh)
(玩家.hh)
#ifndef __PLAYER_H__
# define __PLAYER_H__
#include "Core.hh"
class Object;
class Player : public Object
{
...
};
#endif /* __PLAYER_H__ */
采纳答案by Eitan T
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;
.
问题#1:
你必须只从一个完全声明的类派生,否则编译器不知道该怎么做。
删除前向声明class Object;
。
Problem #2:
You have a circular dependency all over:
问题#2:
你到处都有循环依赖:
- In "Core.hh" you include "Object.hh", "MapLink.hh" and "Player.hh".
- In "Object.hh", "MapLink.hh" and "Player.hh" you include "Core.hh".
- 在“Core.hh”中包含“Object.hh”、“MapLink.hh”和“Player.hh”。
- 在“Object.hh”、“MapLink.hh”和“Player.hh”中包含“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".
- 修改“MapLink.hh”和“PlayerLink.hh”,使它们包含“Object.hh”,而不是“Core.hh”
- 修改“Object.hh”,使其不包含“Core.hh”。
回答by zapredelom
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
编译器必须知道用于继承的类的完整接口。在这种情况下,编译器无法看到您的对象。必须object.hh
在其他文件中包含文件
回答by molbdnilo
Follow the includes:
遵循包括:
Object.hh
-__OBJECT_H__
is definedCore.hh
-__CORE_H__
is definedMapLink.hh
- includesCore.hh
, but the content of that file isn't included because of step 2 and the#ifndef
.Player.hh
- Same as step 3.
Object.hh
-__OBJECT_H__
已定义Core.hh
-__CORE_H__
已定义MapLink.hh
- 包括Core.hh
,但由于第 2 步和#ifndef
.Player.hh
- 与步骤 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'tinherit from a class that hasn't been fully defined.
所以,MapLink.hh
和Player.hh
不能看到的定义,Object
你从它试图继承之前,你不能从尚未完全定义一个类继承。
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
.
修复:特别包含您继承的类的标题。
也就是说,添加#include "Object.hh"
到MapLink.hh
和Player.hh
。