c++ - LNK2019 当试图继承一个类时

标签 c++ inheritance linker-errors 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)

谁能告诉我这里做错了什么?

最佳答案

根据错误消息,您已声明未定义您的 Entity::Entity(void); 您可能忘记在编译/链接时包含该文件及其实现。

编辑:关于样式的一个小问题:在 C++ 中,您通常希望使用类似这样的东西:Entity() 而不是 Entity(void)。在 C 中,void 是必要的,它可以使函数原型(prototype)告诉编译器有关(缺少)参数的信息,而不是仅告诉编译器返回类型的函数声明。但是,C++ 没有任何等同于 C 函数声明的东西; C++ 函数声明总是包含有关参数类型的信息,因此空括号告诉编译器该函数不带参数。

关于c++ - LNK2019 当试图继承一个类时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8154149/

相关文章:

c# - 为什么我不能将 set 访问器添加到覆盖属性?

linker - 未定义的引用,将 Plplot 与 GFortran 链接时出错

c++ - Boost::Test:编译并运行一个 "hello world"程序

c++ - 链接 libncurses++ 和 libncurses 的问题

c++ - 在类名之前使用宏在 C++ 中类

c++ - Visual Studio 2013 中的替代标记(不,和等...)

c++ - 使用现有类设计实现功能的问题

c++ - Objective-C++ 中的复杂类层次结构

c++ - 有效地等待线程池中的所有任务完成

java - Java 中的顶级类是什么?