0

重複の可能性:
C ++の宣言されていない識別子(ただし、宣言されていますか?)

sprite.h(20): error C2065: 'Component' : undeclared identifierコンパイルしようとするとエラーが発生します(他にもいくつかのファイルがあります)。以下はsprite.hファイルです。何がこの問題を引き起こしているのか、私は一生理解できません。

#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

.cppファイルtick()では、次のように定義されています。

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

tick()現在のように2つのベクトルを取ることになっていますが、この問題を解決する可能性のある、より良い方法があるのではないでしょうか。

編集 要求に応じて、ここにComponent.hもあります:

#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
4

1 に答える 1

3

Sprite.hinclude Component.hwhich include Sprite.h、解決できない循環依存関係を与えます。

幸い、ヘッダーを含める必要はまったくありません。各クラスは他のクラスへのポインタのみを参照し、そのためには単純な宣言で十分です。

class Component;
于 2012-12-28T14:44:48.930 に答える