0

I write a game and I need to create a vector of the sprite that represents the enemies in my map struct, but because map.h include sprite.h and vice versa I have an error 'Sprite' is not declared in this scope. I heard about forward declaration; is that the solution?

#ifndef SPRITE_H
#define SPRITE_H

#include "map.h"
#include "anim.h"

struct Sprite
{
    Animation an;
    float x,y;
    int w,h;
    float vx,vy;
    bool air;
    int right, left, top, bottom;
    int sens;
};

void InitSprite(Sprite* sp,Charset* c,float x,float y,int nbstats);
int EssaiDeplacement(Map* m,Sprite *sp,float vx,float vy);
void Affine(Map* m,Sprite *sp,float vx,float vy);
void Move(Map* m,Sprite *sp,const Uint8 *keys);
void RenderSprite(Sprite* sp,Charset* c);
void Tile_Collision(Sprite* sp,Map* m);
void Evolue(Sprite* sp,const Uint8 *state);

#endif
#ifndef MAP_H
#define MAP_H

#include <string>

#include "mario.h"
#include "sprite.h"

struct Map
{
    Charset* c;
    int xtiles_level,ytiles_level;
    std::vector<std::vector<int> >tableau;
    std::vector<int> tiles;
    std::vector<std::vector<int> > limits;
    std::vector<Sprite> enemys;
};

void Load_Tiles(Map* m,Charset* c,const char* fichier,int largeur_monde,int hauteur_monde);
void Load_Level(Map* m,const char* fichier);
void AfficherMap(Map* m,SDL_Renderer* renderer);
void SaveMap(Map* m,const char* fichier);
int TileCollision(SDL_Rect *test,Map* m);

#endif

The error is in map.h.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
venom007
  • 9
  • 2
  • Likely duplicate. https://stackoverflow.com/questions/9906402. – Drew Dormann Sep 27 '19 at 16:01
  • 1
    You tagged this C and C++. Pick one. In C, `struct Sprite` does not declare a type named `Sprite`. In C++, it does. – Eric Postpischil Sep 27 '19 at 16:02
  • Provide a [mcve]. – Eric Postpischil Sep 27 '19 at 16:04
  • `InitSprite(Sprite* sp` looks like C-style OOP. Why do you not declare methods in classes? – 273K Sep 27 '19 at 16:05
  • Odd that map.h includes `string` but has no `std::string` anywhere. And it doesn't include vector, but it has `std::vector`. – Eljay Sep 27 '19 at 16:22
  • all the includes is in mario.h and i need strings for SaveMap function – venom007 Sep 27 '19 at 16:30
  • As a suggestion, it works much better if every source file and every header file includes all of the headers they directly depend upon, and do not include any headers they do not directly depend upon, and avoid having dependencies fulfilled indirectly (because that makes the code very brittle and hard to refactor). – Eljay Sep 27 '19 at 16:33
  • You have heard about forward declarations, but have you tried using them in this code? – JaMiT Sep 27 '19 at 16:55

1 Answers1

0

Seeing cyclic redundancy in your code! Forward declaration should resolve the issue. Another way:

#ifndef COMMON_H
#define COMMON_H

#include "anim.h"

struct Sprite
{
};

struct Map
{
};

#endif

#ifndef SPRITE_H
#define SPRITE_H

#include "common.h"

void InitSprite(Sprite* sp, Charset* c, float x, float y, int nbstats);
/* ... */
#endif

#ifndef MAP_H
#define MAP_H

#include "common.h"
#include "mario.h"
#include "sprite.h"

void Load_Tiles(Map* m, Charset* c, const char* fichier, int largeur_monde, int hauteur_monde);
/* ... */
#endif
seccpur
  • 4,996
  • 2
  • 13
  • 21