Question

Within my code I've created two classes, both with individual header and class files.

So in my "player.h" header I have:

#ifndef PLAYER_H
#define PLAYER_H

#include "timer.h"
#include "funcs.h"

enum state{IDLE, RUN, JUMPRISE, JUMPFALL};

class Player
{
public:
     Player(SDL_Renderer* renderer, SDL_Texture* charSheet, int x, int y, int movespeed, SDL_Rect dimensions);
     void show(Timer animTimer);
     void handle(const Uint8* keydown, SDL_Rect* rex, int numRex);
     int getX();
     int getY();

     ~Player();

private:
     SDL_Renderer* m_renderer;
     SDL_Texture* m_charSheet;
     SDL_Rect m_coords;
     int m_movespeed;
     bool m_direction;

     state animState;

     SDL_Rect anim_left_run_clip[8];
     SDL_Rect anim_left_idle_clip[8];
     SDL_Rect anim_left_jumprise_clip[8];
     SDL_Rect anim_left_jumpfall_clip[8];
     SDL_Rect anim_right_run_clip[8];
     SDL_Rect anim_right_idle_clip[8];
     SDL_Rect anim_right_jumprise_clip[8];
     SDL_Rect anim_right_jumpfall_clip[8];
};

#endif // PLAYER_H

Every time I compile, I get an error saying "error: Timer has not been declared" at the line containing:

void show(Timer animTimer)

My "timer.h" file is nothing special, nor do I believe the problem is located there. Things I've already checked for:

  • "Timer" vs "timer"
  • including "timer.h" at the top of the "player.h" header file
  • correct directory for "timer.h" file

Either I'm overlooking some obvious error somewhere, or I've committed some coding sin that I was not aware of.

Was it helpful?

Solution

Most likely, in timer.h you are #including player.h, which then #includes timer.h also. But because of the header guards, the content of timer.h is then ignored. The consequence is that player.h is parsed ahead of timer.h. The end result is you can't have two clases mutually depending on each others implementation in that way. You need to decide which one is the more basic one that the other uses.

If, in the header, A uses B, but B only uses pointers to A, you can put B first, and then forward declare A with a statement like.

class A;

In that case B.h does not #include A.h for the full class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top