好吧...我已经努力解决了至少一个小时的错误。我的StaticSprite类继承自Component类。但是,当我尝试实例化StaticSprite实例时,出现错误c2016。

这是我的相关代码,如果需要的话,我会发布更多。
提前致谢。

静态精灵

#ifndef STATICSPRITE_H_
#define STATICSPRITE_H_

#include "Component.h"
#include <SFML/Graphics.hpp>
#include <string>

namespace GE
{
 class Entity;
 class Component;
 class StaticSprite : public Component
 {
 private:
  sf::Image image;
 public:
  sf::Sprite Sprite;
  StaticSprite();
  StaticSprite(std::string filename);
 };
}

#endif

组件.h
#ifndef COMPONENT_H_
#define COMPONENT_H_

#include "Entity.h"
#include "ComponentType.h"
#include <iostream>

namespace GE
{
 class Entity;
 class Component
 {
 protected:
  Entity* myEntity;
  TYPE type;
 public:
  Component();
  virtual ~Component() {}
  Entity* getEntity();
 };
}

#endif

main.cpp
int main(int argc, char* argv[])
{
    Entity Player;
    //The first arg is just a number, and the second is the
    //actual component to add to the Entity's component array
    Player.addComponent(StaticSprite, new StaticSprite()); //error

    //application loop
}

实体.cpp
//TYPE is just an enumerated type
void Entity::addComponent(TYPE type, void* component)
{
Components[type] = component;
}

ComponentType.h
#ifndef COMPONENTTYPE_H_
#define COMPONENTTYPE_H_

namespace GE
{
enum TYPE{Base = 0, StaticSprite, DynamicSprite,
    Physics, Collision, Input, Particle, Audio, Scriptable, MaxType};
}

#endif

最佳答案

如果您仍然包含class Component;,那么声明Component.h有什么意义?您不能从不完整的类型继承,因此此声明没有任何意义。

大胆的猜测是循环包含问题,但是如果没有更多信息,很难说出来。如果您对此主题不清楚,建议阅读有关在C和C++中组织代码文件的this article

编辑:



您不能将StaticSprite类型传递给方法。这可能是错误的根源。向我们展示addComponent的声明。第一个参数类型是什么?

编辑:



好的,TYPE的定义是什么?我想这只是int的typedef还是什么?在这种情况下,很明显您不能将StaticSprite类型分配给int

编辑:



是的,那里有。枚举器TYPE::StaticSpriteStaticSprite类型绝对无关,即使它们具有相同的名称。为枚举器指定其他名称,例如T_StaticSprite,以下代码应该可以工作:



(是否整个枚举类型都是一个好主意,这是一个不同的问题。)

08-05 05:42