【发布时间】: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++