包含文件包含自身与守卫

问题描述

我有两个头文件animation.hhero.h, 这是 animation.h代码

#include <SFML/Graphics.hpp>
#include <iostream>
#include "hero.h"

#ifndef ANIMATION
#define ANIMATION

//Class

#endif

对于hero.h

#include <SFML/Graphics.hpp>
#include <iostream>
#include "animation.h"

#ifndef HERO
#define HERO

//Class

#endif

即使使用包含保护,我也收到错误消息 #include file "" includes itself。 我刚开始使用包含守卫等,所以这可能是一个非常微不足道的错误

感谢您的帮助

解决方法

你应该把头卫放在其他任何东西之前。

gccMSC 上,您也可以使用

#pragma once

相反。可能也在其他更现代的编译器上。只需将它放在包含的顶部,而不是 #ifdef...

animation.h

#ifndef ANIMATION
#define ANIMATION

#include <SFML/Graphics.hpp>
#include <iostream>
#include "hero.h"

//Class

#endif

hero.h

#ifndef HERO
#define HERO

#include <SFML/Graphics.hpp>
#include <iostream>
#include "animation.h"

//Class

#endif

animation.h

#pragma once

#include <SFML/Graphics.hpp>
#include <iostream>
#include "hero.h"

//Class

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...