因此,我正在尝试从另一个类继承一个类。 我有基类 Entity,我有一个需要从它继承的 Hero 类。
像往常一样,我这样做:
#include "Entity.h"
class Hero : public Entity
{
public:
Hero(Sprite* sprite_, Scene* scene, float xPosition, float yPosition, const char* name);
~Hero(void);
};
我的实体类:
#include "Sprite.h"
#include <vector>
#include "Scene.h"
class Entity
{
public:
Entity(void);
Entity(Sprite* Sprite_);
Entity(Sprite* Sprite_, Scene* scene, float xPosition, float yPosition, const char*);
~Entity(void);
}
我得到的错误是:
1>Hero.obj : error LNK2019: unresolved external symbol "public: __thiscall Entity::Entity(void)" (??0Entity@@QAE@XZ) referenced in function "public: __thiscall Hero::Hero(class Sprite *,class Scene *,float,float,char const *)" (??0Hero@@QAE@PAVSprite@@PAVScene@@MMPBD@Z)
谁能告诉我这里做错了什么?
最佳答案
根据错误消息,您已声明未定义您的 Entity::Entity(void);
您可能忘记在编译/链接时包含该文件及其实现。
编辑:关于样式的一个小问题:在 C++ 中,您通常希望使用类似这样的东西:Entity()
而不是 Entity(void)
。在 C 中,void
是必要的,它可以使函数原型(prototype)告诉编译器有关(缺少)参数的信息,而不是仅告诉编译器返回类型的函数声明。但是,C++ 没有任何等同于 C 函数声明的东西; C++ 函数声明总是包含有关参数类型的信息,因此空括号告诉编译器该函数不带参数。
关于c++ - LNK2019 当试图继承一个类时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8154149/