0

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

Kerch
  • 25
  • 6
  • Why does animation include hero? I think that a hero requires animation, but animation wouldn't know about hero or any other animated entity. So you should include hero in the cpp file where it is needed, instead of animation (like hero.cpp) – Devolus Apr 12 '21 at 05:37

1 Answers1

3

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
Devolus
  • 21,661
  • 13
  • 66
  • 113
  • 1
    That might remove the warning and the immediate problem, but it won’t fix the real problem. – Pete Becker Apr 11 '21 at 22:22
  • And note that labeling `#pragma once` “modern” doesn’t make it work. There are situations (for example, source files stored on multiple network servers) where the compiler might not be able to tell whether two include files are “the same” file. – Pete Becker Apr 11 '21 at 22:25