LNK2019 当尝试继承一个类时
所以,我试图从另一个类继承一个类。 我有基类 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)
谁能告诉我我在这里做错了什么?
So, I'm trying to inherit a class from another.
I have the base class Entity and I have a Hero class that needs to be inherited from it.
Like usual, I do this like this:
#include "Entity.h"
class Hero : public Entity
{
public:
Hero(Sprite* sprite_, Scene* scene, float xPosition, float yPosition, const char* name);
~Hero(void);
};
My entity class:
#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);
}
And the erorr I get is:
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)
Could anyone tell me what I'm doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从错误消息中可以看出,您已声明为未定义
Entity::Entity(void);
您可能忘记在编译/链接时包含该文件及其实现。编辑:关于样式的一个小问题:在 C++ 中,您通常想要使用类似:
Entity()
而不是Entity(void)
。在 C 中,void
是必要的,它可以使函数原型告诉编译器有关(缺少)参数的信息,而不是仅告诉编译器返回类型的函数声明。然而,C++ 没有任何与 C 函数声明等效的东西。 C++ 函数声明总是包含有关参数类型的信息,因此空括号告诉编译器该函数不带参数。From the error message, you've declared by not defined your
Entity::Entity(void);
You've probably forgotten to include the file with its implementation when you compiled/linked.Edit: One minor aside on style: in C++, you usually want to use something like:
Entity()
rather thanEntity(void)
. In C, thevoid
is necessary to make that a function prototype that tells the compiler that about the (lack of) parameters, rather than a function declaration that only tells the compiler the return type. C++, however, doesn't have anything equivalent to a C function declaration; a C++ function declaration always includes information about the parameter types, so an empty parentheses tells the compiler that the function takes no parameters.您缺少 Entity::Entity(void); 的实现;
You're missing an implementation for
Entity::Entity(void);
该错误意味着它无法将声明链接到实际的函数实现。
这通常意味着您没有实现它,或者您实现了您认为是的东西,但实际上签名略有不同。
That error means that it couldn't link the declaration to an actual function implementation.
Which usually means you didn't implement it, or you implemented something you thought was it, but actually with a slightly different signature.