سؤال

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?

هل كانت مفيدة؟

المحلول

You're missing an implementation for Entity::Entity(void);

نصائح أخرى

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 than Entity(void). In C, the void 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.

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top