简体   繁体   English

抽象类和继承C ++

[英]Abstract classes and inheritence C++

I've got a question I'm rather confused by. 我有一个我很困惑的问题。 Right now, I've got an abstract GameObject class, and a Sprite2D class that is inherited from the GameObject class. 现在,我有一个抽象的GameObject类和一个从GameObject类继承的Sprite2D类。

However, I found out that, when I try to implement additional functions that are not defined in the GameObject class, the linker throws LNK2001: Unresolved External Symbol errors. 但是,我发现,当我尝试实现GameObject类中未定义的其他功能时,链接器将引发LNK2001:Unresolved External Symbol错误。 They go away when I declare pure virtual definitions of the new functions in GameObject, though. 但是,当我在GameObject中声明新功能的纯虚拟定义时,它们消失了。

Why is that? 这是为什么? Are there any useful sources that I can turn to to get a better understanding of the relationship between abstract classes and inheritance? 是否有任何有用的资源可以用来更好地理解抽象类和继承之间的关系? Thanks! 谢谢!

PS For those who are interested, my GameObject and Sprite2D headers are below. PS对于感兴趣的人,下面是我的GameObject和Sprite2D标头。

GameObject.h: 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: 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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM