错误"未在作用域中声明",但带有接口

Error "not declared in the scope" but with interfaces

本文关键字:接口 作用域 错误 声明      更新时间:2023-10-16

你看我一直在尝试创建一个std::vector,其中包含IState类中的Entity类。这两个类都是接口。

错误是

在此范围内未声明"实体"

它指向:

protected:
    std::vector <Entity*> ent_map;

在IState.h内部

我已经尝试了几个小时来解决它。一旦我在 IState.h 中做了一个前向声明,但一旦我这样做并尝试使用向量,它就会喷出它是一个不完整的类,所以我又回到了原点。

有什么想法吗?

实体.h

#ifdef __ENTITY__
#define __ENTITY__
#include <iostream>
#include <SDL.h>
class Entity
{
    public:
    virtual ~Entity();
    virtual void load(const char* fileName, std::string id, SDL_Renderer* pRenderer) = 0;
    virtual void draw() = 0;
    virtual void update() = 0 ;
    virtual void clean() = 0;
    /*void int getX() { return m_x;}
    void int getY() { return m_y;}
    void std::string getTexID {return textureID;}
    */

};

#endif // __ENTITY__

IState.h

#ifndef IState_
#define IState_
#include "Entity.h"
#include <vector>
class IState
{
    public :
    virtual ~IState();
    virtual void update() = 0;
    virtual void render(SDL_Renderer* renderTarget) = 0;
    virtual bool onEnter() = 0;
    virtual bool onExit() = 0;
    virtual void handleEvents(bool* gameLoop,SDL_Event event) = 0;
    virtual void resume() = 0;
    virtual std::string getStateID() = 0;
    virtual void setStateID(std::string id) = 0;
    protected:
        std::vector <Entity*> ent_map;
};

#endif // IState_

"Entity.h"的内容根本不包括在内。

改变

#ifdef __ENTITY__

#ifndef __ENTITY__

顺便说一句:名称包含双下划线或以下划线开头,后跟大写字母保留在C++中,您需要小心。