简体   繁体   中英

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.

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. They go away when I declare pure virtual definitions of the new functions in GameObject, though.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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