无法使用标头保护解决包含其自身的文件(包括标头)的错误
Can't solve error with file including header that includes itself using header guards
我有一个名为Application.h的头文件,其中包含一个名为CollisionHandler.h的头。CollisionHandler包含Application.h,因此在编译时会出现过多包含错误。为了解决这个问题,我在标头保护之间添加了CollisionHandler,如下所示:
#ifndef COLLISION_HANDLER_INCLUDED_H
#define COLLISION_HANDLER_INCLUDED_H
#include "CollisionHandler.h"
#endif
但是,当我尝试使用类型为CollisionHandler的对象(该类在CollisionHandler.h中定义,在标头保护之间)作为Application类(在Application.h中也在标头保护间定义)的成员变量时,我会对包括Application.h的每个文件重复此错误(5次左右):
1>c:usersaitordocumentsvisual studio 2008projectscoptercopterapplication.h(19) : error C2143: syntax error : missing ';' before '*'
1>c:usersaitordocumentsvisual studio 2008projectscoptercopterapplication.h(19) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:usersaitordocumentsvisual studio 2008projectscoptercopterapplication.h(19) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
第19行是我将CollisionHandler对象声明为成员变量的行。
以下是Application.h中的代码(已识别相关行):
#include "Header.h"
#include <stdio.h>
#include "GameCharter.h"
#include <vector>
#include <boost/shared_ptr.hpp>
#ifndef COLLISION_HANDLER_INCLUDED_H //Here I include
#define COLLISION_HANDLER_INCLUDED_H //the collision
#include "CollisionHandler.h" //handler header
#endif
using namespace std;
#ifndef APPLICATION_DEFINED_H
#define APPLICATION_DEFINED_H
class LimitsManager;
class ObstacleManager;
class Application
{
public:
CollisionHandler *handler; //Here I declare the CollisionHandler
ObstacleManager *obstacleManager;
LimitsManager *limitsManager;
vector<boost::shared_ptr<GameChar> > characters;
vector<int> idsToRemove;
void gameLoop();
Application();
bool idsNeedUpdate;
bool objectsNeedToRemove;
};
#endif
这是CollisionHandler的代码。h:
#include "Header.h"
#include "Application.h"
#include "GameCharter.h"
#include "LimitObstacle.h"
#ifndef COLLISION_HANDLER_H
#define COLLISION_HANDLER_H
class CollisionHandler
{
Application *app;
public:
void handleCollisions();
CollisionHandler()
{
}
CollisionHandler(Application *app);
bool collidersAreCollidingGeneral(GameChar* char1,GameChar* char2);
bool collidersAreCollidingSecondary(GameChar* char1,GameChar* char2);
};
#endif
此外,如果我在Application.h中使用class CollisionHandler;
,然后在cpp文件中包含CollisionHandler.h,则它可以使用
循环包含标头是错误的,即使使用include保护也是如此。
尽可能使用前向声明来删除依赖项。
如果不能使用正向声明,那么您的设计就是有缺陷的。
您所需要做的就是添加/写入
pragma once
在每个头文件的顶部
相关文章:
- C++系统找不到指定的文件错误
- C++语法头文件错误
- C++生成文件错误"implicit entry/start for main executable"
- C++/生成文件错误:未定义对"main"的引用
- VS2019:资源文件错误:中性(默认)(未知子语言:0x8)
- C++程序不制作文件(错误)
- 安卓工作室 |CPP 文件错误错误: 位图库中对"AndroidBitmap_unlockPixels"的未定义引用
- "what does ":*** [可执行文件] 错误 1 " mean ?"
- 如何修复使用VScode调试器gcc调试时的"找不到文件"错误
- 在课堂上创建了一个朋友,给出"无输入文件"错误
- Qt .exe文件错误
- c++ 添加具有写入文件错误的文本变量
- 无法转换 .CATPart 文件.错误:输入文件路径似乎包含不支持的字符
- 写入文本文件错误,QT
- C++写入文件错误
- Windows Subsystem for Linux (WSL) 下的间歇性随机"找不到文件"错误
- 犰狳读取MAT文件错误
- 无法求解FullPathForFilename:COCOS2DX中可能缺少文件错误
- C Wininet FTP列出了许多文件错误
- 生成文件错误 - 找不到文件 - *.cpp