0

Possible Duplicate:
C++ Undeclared Identifier (but it is declared?)

Im getting the error sprite.h(20): error C2065: 'Component' : undeclared identifier when I try to compile (I got a couple other files as well). Below is the sprite.h file. I cant for the life of me figure out what is causing this problem.

#ifndef SPRITE_H
#define SPRITE_H

#include "Image.h"
#include "Rectangle.h"
#include <string>
#include <SDL.h>
#include <vector>
#include "Component.h"

namespace GE2D {

    class Sprite {
    public:
        Sprite();
        Sprite(Image *i);
        Sprite(Image *i, int x, int y);
        Sprite(char *file, bool transparentBg, int x, int y, int w, int h);
        virtual ~Sprite();
        virtual void tick(SDL_Surface *screen, std::vector<Sprite*>* sprites, std::vector<Component*>* components);
        virtual void handleEvent(SDL_Event eve);
        virtual void draw(SDL_Surface *screen);
        void setPosition(int x, int y);
        const Rectangle& getRect() const;
        const Image& getImage() const;
        const Sprite& operator=(const Sprite& other);
        Sprite(const Sprite& other);
    protected:

    private:
        Image image;
        Rectangle rect;
    };

}
#endif

In the .cpp file tick() is defined like this:

void Sprite::tick(SDL_Surface *screen, std::vector<Sprite*>* sprites, std::vector<Component*>* components) {}

tick() is supposed to take two vectors like they do now, but maybe there's a better way to do that which might solve this problem?

EDIT As requested, here is Component.h as well:

#ifndef COMPONENT_H
#define COMPONENT_H

#include "Rectangle.h"
#include "Component.h"
#include "Sprite.h"
#include <vector>
#include <SDL.h>

namespace GE2D {

    class Component {
    public:
        Component();
        virtual ~Component();
        virtual void draw(SDL_Surface *screen) = 0;
        virtual void tick(SDL_Surface *screen, std::vector<Sprite*>* sprites, std::vector<Component*>* components) = 0;
        virtual void handleEvent(SDL_Event eve) = 0;
        const Rectangle& getRect() const;

    protected:
        Component(int x, int y, int w, int h);
    private:
        Rectangle rect;
    };

}
#endif
Community
  • 1
  • 1
Topsic
  • 109
  • 1
  • 9

1 Answers1

3

Sprite.h includes Component.h which includes Sprite.h, giving a circular dependency which can't be resolved.

Luckily, you don't need to include the headers at all. Each class only refers to a pointer to the other class, and for that a simple declaration is enough:

class Component;
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • But shouldnt the include guards solve this? Isnt that their purpose? Edit: Oh nvm I saw your answer – Topsic Dec 28 '12 at 14:46
  • @Ceilingbat: No, they only prevent multiple inclusions. There is no way to both define `Sprite` before `Component` *and* `Component` before `Sprite`. – Mike Seymour Dec 28 '12 at 14:50
  • Where should the declaration be? – Topsic Dec 28 '12 at 14:50
  • @Ceilingbat: Declare `Component` before the definition of `Sprite`, inside the namespace, and vice versa. – Mike Seymour Dec 28 '12 at 14:51
  • @Ceilingbat The include guards only stop it from infinitely recursively including. Start with Component.h and in your head do the inclusions. Notice that Sprite will not have a definition of Component above it. – Joseph Mansfield Dec 28 '12 at 14:51
  • Thanks, I solved it. I didnt know about forward declarations before, will start using them where appropriate. – Topsic Dec 28 '12 at 15:10