错误 - 无效使用不完整的类型/前向声明
Error - invalid use of incomplete type / forward declaration of
我知道我的问题很常见,但我一直在搜索并尝试我找到的每一个解决方案,但仍然不起作用。因此,任何帮助将不胜感激!=)
提前感谢!
我在编译时遇到此错误:
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
所以基本上,我有一个主要包含(主要.cpp)
#include "Core.hh"
int main(void)
{
...
}
这是包含我所有包含的头文件 (Core.hh)
#ifndef __CORE_HH__
# define __CORE_HH__
#include ...
#include "Object.hh"
#include "MapLink.hh"
#include "Player.hh"
class Core
{
...
};
#endif /* __CORE_HH__ */
然后是给我带来麻烦的文件(对象.hh)
#ifndef __OBJECT_HH__
# define __OBJECT_HH__
#include "Core.hh"
class Object
{
...
};
#endif /* __OBJECT_HH__ */
(地图链接.hh)
#ifndef __MAPLINK_H__
# define __MAPLINK_H__
#include "Core.hh"
class Object;
class MapLink : public Object
{
...
};
#endif /* __MAPLINK_H__ */
(玩家.
#ifndef __PLAYER_H__
# define __PLAYER_H__
#include "Core.hh"
class Object;
class Player : public Object
{
...
};
#endif /* __PLAYER_H__ */
问题 #1:
必须仅从完全声明的类派生,否则编译器将不知道该怎么做。
删除前向声明class Object;
。
问题#2:
你有一个循环依赖关系:
- 在"Core.hh"中,您可以包括"Object.hh","MapLink.hh"和"Player.hh"。
- 在"Object.hh","MapLink.hh"和"Player.hh"中,您包括"Core.hh"。
您需要确保每个类都完全包含它继承自的类。
我不确定这些类如何相互作用,您应该在问题中提供该详细信息。
我的猜测是您需要修改您的内含物,如下所示:
- 修改"MapLink.hh"和"PlayerLink.hh",使其包含"Object.hh",而不是"Core.hh"
- 修改"Object.hh",使其不包含"Core.hh"。
编译器
必须知道继承类的完整接口。在这种情况下,编译器看不到您的对象。有必要在其他文件中包含object.hh
文件
遵循以下内容:
-
Object.hh
- 定义__OBJECT_H__
-
Core.hh
- 定义__CORE_H__
-
MapLink.hh
- 包括Core.hh
,但由于步骤 2 和#ifndef
,不包括该文件的内容。 -
Player.hh
- 与步骤 3 相同。
因此,在尝试从Object
继承之前,MapLink.hh
和Player.hh
无法看到它的定义,并且您不能从尚未完全定义的类继承。
解决方法:具体包括要继承的类的标头。
也就是说,将#include "Object.hh"
添加到MapLink.hh
和Player.hh
。
相关文章:
- 将类型声明为类型模板参数的模板参数的一部分是否合法?
- 当返回类型声明为 ListNode 时,我们是否可以返回 false<T>*
- 如何使用自定义类型声明Arduino数组?
- 将函数参数类型声明为 auto
- 使用typedef'ed返回类型声明友元函数时出现编译器错误
- 在没有标识符的情况下如何读取复杂的C++类型声明?
- 模板函数参数显式类型声明
- 使用指针遍历一系列基元类型声明?
- 字符类型声明和验证
- 使用本地类型声明的G lambda被使用但从未定义 - 确实是一个错误
- 为什么 c++ lambda 捕获不需要类型声明?
- 为什么没有"int"类型声明时输出不同?
- C++如何使用虚拟基类型声明全局静态分配的变量
- 我可以使用预处理器将一个类型声明替换为另一个类型声明吗?
- 如何将数据类型声明从.cpp文件传输到 .cu 文件
- C++17 枚举类型声明
- 条件类型声明
- 具有特定参数的特定构造函数的类型声明
- 我可以使用相同的名称为周围作用域中的类型声明成员类型别名吗
- 函数调用之前,C 类型声明