抽象类与继承C++



我有一个问题,我很困惑。现在,我得到了一个抽象的GameObject类和一个从GameObject类继承的Sprite2D类。

然而,我发现,当我试图实现GameObject类中未定义的其他函数时,链接器会抛出LNK2001:未解决的外部符号错误。不过,当我在GameObject中声明新函数的纯虚拟定义时,它们就消失了。

为什么?有什么有用的来源可以让我更好地理解抽象类和继承之间的关系吗?谢谢

附言:对于那些感兴趣的人,我的GameObject和Sprite2D标题如下。

GameObject.h:

#ifndef GAMEOBJECT_H
#define GAMEOBJECT_H
class GameObject
{
private:
    class Concept
    {
    public:
        virtual void Update() = 0;
        virtual void Draw() = 0;
    };
    template <typename T>
    class Model : public Concept
    {
    public:
        Model(T base) : newObj(base)
        {
        }
        void Update()
        {
            newObj.Update();
        }
        void Draw()
        {
            newObj.Draw();
        }
    private:
        T newObj;
    };
    Concept *pC;
public:
    template <typename T>
    GameObject(T newObj) : pC(new Model <T>(newObj))
    {
    }
    void Update()
    {
        pC->Update();
    }
    void Draw()
    {
        pC->Draw();
    }
};
#endif

Sprite2D.h:

#ifndef SPRITE2D_H
#define SPRITE2D_H
#include "GameObject.h"
#include "AEEngine.h"
#include <string>
class Sprite2D : public GameObject
{
protected:
    std::string name;
    AEVec2 position, scale, direction, velocity, acceleration;
    f32 rotation{ 0.0f };
    AEGfxVertexList * pMesh{ nullptr };
    AEGfxTexture * pTex{ nullptr };
    struct AABB{ AEVec2 min; AEVec2 max; } boundingBox;
    bool isAlive;
    void CreateMesh(AEVec2, AEVec2, u8, u8, u8, u8);
    void CreateMesh(AEVec2, AEVec2, const char *);
public:
    //Constructors
    Sprite2D();
    Sprite2D(const char *name, f32 xPos, f32 yPos, f32 xSize, f32 ySize, u8 alpha, u8 red, u8 green, u8 blue);
    Sprite2D(const char *name, f32 xPos, f32 yPos, f32 xSize, f32 ySize, const char *texPath);
    //Destructor
    ~Sprite2D();
    virtual void Update();
    virtual void Draw();
    void SetAlive(bool);
    bool IsAlive();
    void SetName(std::string);
    std::string GetName();
    void SetPosition(AEVec2);
    AEVec2 GetPosition();
    void SetScale(AEVec2);
    AEVec2 GetScale();
    void SetDirection(AEVec2);
    AEVec2 GetDirection();
    void SetRotation(f32);
    f32 GetRotation();
    void SetVelocity(AEVec2);
    AEVec2 GetVelocity();
    void SetAcceleration(AEVec2);
    AEVec2 GetAcceleration();
    void SetBoundingBox(f32 sizeX, f32 sizeY);
    AABB GetBoundingBox();
};
#endif

确保您实际实现了这些方法,并且实现这些方法的文件包含在您构建的目标中

最新更新