[英]C++ Correct way to use headers
I'm by no means new to using headers in programs, but I've recently run into a strange problem. 我在程序中使用标头绝不是新手,但是我最近遇到了一个奇怪的问题。
#ifndef PLAYER_HEADER_GUARDS
#define PLAYER_HEADER_GUARDS
#include "CollidableObject.h"
#include "Includes.h"
extern const int ANIMATION_EXTENDER;
extern class MapSystem *mapSystem;
class Player : public CollidableObject
{
//declaration of rest of the class
};
#endif
I have a lot of classes split across several files (and their respective headers), but here is my current problem : even though I have already included CollidableObject.h, the compiler (CodeBlocks) throws an error 我有很多类分散在多个文件(及其各自的头)中,但这是我当前的问题:即使我已经包含了CollidableObject.h,编译器(CodeBlocks)也会引发错误
Player.h|12|error: expected class-name before '{' token|
The problem arises from the " : public CollidableObject" part of the code, since the program worked fine before I added that. 问题出在代码的“:public CollidableObject”部分,因为在我添加该程序之前,该程序运行良好。 Shouldn't including the CollidableObject header (I'm not posting it, but it basically looks just the same, and defines the class CollidableObject) remove this kind of a problem?
是否不应该包含CollidableObject标头(我没有在其中发布它,但它看起来基本相同,并且定义了类CollidableObject)消除了这种问题? Further,
进一步,
extern class MapSystem *mapSystem;
This statement should, to the best of my knowledge, compile fine without the extra "class" after extern, but if I remove the class keyword, it throws an error "expected type qualifier before * token". 据我所知,该语句应在extern之后没有额外的“类”的情况下进行编译,但是如果我删除class关键字,它将抛出错误“ *标记之前的预期类型限定符”。 The class MapSystem has also been defined previously, and it should work fine - but without the class keyword, it doesn't.
MapSystem类也已在前面定义,并且可以正常工作-但没有class关键字,则不能。
So that brings me to my question - what mistake am I making in my header files, that is leading to such problems? 因此,我想到了一个问题-我在头文件中犯了什么错误,导致了这些问题? No multi-filed project that I worked on before had such problems.
我以前从事的多文件项目都没有这样的问题。
It sounds like either "CollidableObject.h"
or "Includes.h"
tries to reinclude "Player.h"
, resulting in a circular dependency. 听起来
"CollidableObject.h"
或"Includes.h"
试图重新"Includes.h"
"Player.h"
,导致循环依赖。 The result is that Player
ends up being defined before CollidableObject
, so the latter name is not declared at that point. 结果是
Player
最终在CollidableObject
之前被定义,因此在那一刻不声明后者。
Make sure neither of these headers, nor any of their dependencies, include "Player.h"
. 确保这些标头
"Player.h"
任何依赖项"Player.h"
包含"Player.h"
。 You might need to add forward declarations of class Player;
您可能需要添加
class Player;
声明class Player;
if anything needs to use that name. 如果需要使用该名称。
Also check that "CollidableObject.h"
does actually define class CollidableObject
(perhaps there's a spelling mistake?), and has a uniquely named include guard. 还要检查
"CollidableObject.h"
是否确实定义了class CollidableObject
(可能存在拼写错误?),并且具有唯一命名的包含保护。
As for the second question: 至于第二个问题:
extern class MapSystem *mapSystem;
This doubles up as a declaration of class MapSystem
, so doesn't require a previous declaration. 这可以作为
class MapSystem
的声明class MapSystem
,因此不需要之前的声明。
extern MapSystem *mapSystem;
This does not declare MapSystem
(it only indicates that it's a type name, not a class), so it does require a previous declaration. 这不会声明
MapSystem
(仅指示它是类型名称,而不是类),因此它确实需要先前的声明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.