【问题标题】:Error - invalid use of incomplete type / forward declaration of错误 - 不完整类型的无效使用/前向声明
【发布时间】:2012-06-28 21:44:29
【问题描述】:

我知道我的问题很常见,但我一直在搜索并尝试找到的所有解决方案,但仍然无法正常工作。所以任何帮助将不胜感激! =)

提前致谢!

我在编译时遇到这个错误:

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

所以基本上,我有一个包含 (main.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__ */

然后是给我带来麻烦的文件(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__ */

(播放器.hh)

#ifndef __PLAYER_H__
# define __PLAYER_H__

#include "Core.hh"

class Object;

class Player : public Object
{
  ...
};

#endif /* __PLAYER_H__ */

【问题讨论】:

  • 你不应该使用reserved names作为标题保护;它可能会导致类似stackoverflow.com/questions/3345159 的问题
  • 这是我自己的情况的解决方案,您的标题中存在相同的错误,但可能不适合您:如果该类的公共方法是,我必须在我的 cpp 中包含该类的标题投诉由另一个类引用。我也有非常复杂的依赖关系以及前向声明和包含的混合使用。我在这里写它更多的是作为一个笔记而不是一个解决方案,因为你已经有了一个。

标签: c++ inheritance compiler-errors g++


【解决方案1】:

问题 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”。

【讨论】:

  • Core.hh/Object.hh中没有相互引用
  • 我想获得所有的 SFML,但你是对的,包括整个 Core.hh 是个坏主意!试试看
  • 这样好多了!非常感谢 Eitan 先生:D
【解决方案2】:

编译器必须知道类的完整接口才能继承。在这种情况下,编译器看不到您的对象。需要在其他文件中包含object.hh文件

【讨论】:

  • 感谢您的回复!抱歉,我忘了提到“Object.hh”已经包含在 Player.cpp 和 MapLink.cpp 中并且仍然出现该错误!
【解决方案3】:

遵循包括:

  1. Object.hh - __OBJECT_H__ 已定义
  2. Core.hh - __CORE_H__ 已定义
  3. MapLink.hh - 包括 Core.hh,但由于第 2 步和 #ifndef,不包括该文件的内容。
  4. Player.hh - 与第 3 步相同。

所以MapLink.hhPlayer.hh 在你尝试从它继承之前看不到Object 的定义,你不能从一个没有'没有被完全定义。

解决方法:特别包含您要继承的类的标头。
即,将#include "Object.hh" 添加到MapLink.hhPlayer.hh

【讨论】:

  • 感谢您的帮助!它给出了同样的错误..我不明白这么简单的事情给我带来了这么多麻烦!
猜你喜欢
  • 1970-01-01
  • 2011-10-22
  • 1970-01-01
  • 2014-02-02
  • 2019-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多