Include file includes itself with guards

336 Views Asked by At

I have two header files, animation.h and hero.h, here is the code for animation.h:

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

#ifndef ANIMATION
#define ANIMATION

//Class

#endif

And for hero.h:

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

#ifndef HERO
#define HERO

//Class

#endif

I get the error message #include file "" includes itself even when using include guards. I am new to using include guards and such so this may be a very trivial mistake.

Thanks for any help

1

There are 1 best solutions below

2
Devolus On BEST ANSWER

You should put the header guards before anything else.

On gcc and MSCyou can also use

#pragma once

instead. Probably also on other more modern compilers. Simpliy put this at the top of your include, instead of the #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

Or animation.h

#pragma once

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

//Class