抽象类和继承C ++

时间:2015-02-05 08:51:27

标签: c++ inheritance abstract-class

我有一个问题,我很困惑。现在,我有一个抽象的GameObject类,以及一个继承自GameObject类的Sprite2D类。

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

为什么?是否有任何有用的资源可以让我们更好地理解抽象类和继承之间的关系?谢谢!

P.S。对于那些感兴趣的人,我的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

1 个答案:

答案 0 :(得分:0)

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

相关问题