c++ - 错误 "not declared in the scope"但有接口(interface)

标签 c++ macros include-guards

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

错误是

'Entity' was not declared in this scope

它指向:

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

在 IState.h 中

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

有什么想法吗?

实体.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++ 中是保留的,您需要小心。

关于c++ - 错误 "not declared in the scope"但有接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37723390/

相关文章:

c++ - 通过 boost lambda 占位符访问成员变量

c++ - 编写输出文件 Cuda C++

c - C 中宏的问题

c++ - pragma once inside 和 outside 之间的区别包括守卫?

c++ - Qt 套接字问题

c++ - 使用 QProgressBar : Cannot create children for a parent that is in a different thread 时

java - 为什么Java没有宏?

检查 C 预处理器中的串联定义

c++ - 有没有什么情况你不想包括 guard ?

c - 为什么不允许多次定义 C 结构体的成员?