gpt4 book ai didi

c++ - 抽象类和继承 C++

转载 作者:太空宇宙 更新时间:2023-11-04 13:39:19 25 4
gpt4 key购买 nike

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

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

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

附言对于那些感兴趣的人,我的 GameObject 和 Sprite2D header 在下面。

游戏对象.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

最佳答案

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

关于c++ - 抽象类和继承 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28339678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com