【问题标题】:Error "not declared in the scope" but with interfaces错误“未在范围内声明”但带有接口
【发布时间】:2016-06-09 10:28:44
【问题描述】:

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

错误是

“实体”未在此范围内声明

它指向:

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

在 IState.h 中

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

有什么想法吗?

Entity.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_

【问题讨论】:

  • this。您需要将__ENTITY__ 更改为其他内容。

标签: c++ macros include-guards


【解决方案1】:

"Entity.h"的内容根本不会被收录。

改变

#ifdef __ENTITY__

#ifndef __ENTITY__

BTW:名称中包含双下划线或下划线后跟大写字母在C++中是保留的,需要小心。

【讨论】:

  • 这是一个保留名称。
  • @juanchopanza - 公平地说,OP 首先使用了标识符。
  • @KarlNicoll 是的,但这是需要修复的其他问题。
  • @juanchopanza - 绝对同意 :-)
  • @juanchopanza 顺便说一句,宏可以吗?
猜你喜欢
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-10
  • 2015-06-23
  • 2010-10-02
相关资源
最近更新 更多