gpt4 book ai didi

c++ - 编译器错误 LKN2019 : Unresolved external symbol when dealing with inheritance

转载 作者:太空宇宙 更新时间:2023-11-04 11:58:48 24 4
gpt4 key购买 nike

我在 C++ 中有以下文件:

对象.h:

class Object{
public:
Object(void);
~Object(void);

GLfloat getLeft();
GLfloat getRight();
GLfloat getBottom();
GLfloat getTop();
};

播放器.h:

    class Player : public Object{
public:
Player(void);
~Player(void);

GLfloat getLeft();
GLfloat getRight();
GLfloat getBottom();
GLfloat getTop();
};

平台.h:

class Platform : public Object{
public:
Platform(void);
~Platform(void);

GLfloat getLeft();
GLfloat getRight();
GLfloat getBottom();
GLfloat getTop();
};

支柱.h:

class Pillar : public Object{
public:
Pillar(void);
~Pillar(void);

GLfloat getLeft();
GLfloat getRight();
GLfloat getBottom();
GLfloat getTop();
};

碰撞.h:

#include "Player.h"
#include "Platform.h"
#include "Pillar.h"

class Collision
{
public:
Collision(void);
~Collision(void);
bool CollisionDetect(Object* obj1, Object* obj2);
};

碰撞.cpp:

bool Collision::CollisionDetect(Object* obj1, Object* obj2){
return !(obj2->getLeft() > obj1->getRight()
|| obj2->getRight() < obj1->getLeft()
|| obj2->getTop() > obj1->getBottom()
|| obj2->getBottom() < obj1->getTop());
}

CollisionDetect 将比较 2 个不同的对象是否发生碰撞,为此我需要特定的 x 和 y 值,但是,对象的类型可能会根据不同的情况而有所不同,并且每种类型的对象都有不同的方法来找到它们的 x和 y 值。这意味着我不得不在它们的子类中重新定义相关的 get 方法,但是当我运行这段代码时,我最终遇到了错误:

1>Collision.obj : error LNK2019: unresolved external symbol "public: float __thiscall Object::getLeft(void)" (?getLeft@Object@@QAEMXZ) referenced in function "public: bool __thiscall Collision::CollisionDetect(class Object *,class Object *)" (?CollisionDetect@Collision@@QAE_NPAVObject@@0@Z)
1>Collision.obj : error LNK2019: unresolved external symbol "public: float __thiscall Object::getRight(void)" (?getRight@Object@@QAEMXZ) referenced in function "public: bool __thiscall Collision::CollisionDetect(class Object *,class Object *)" (?CollisionDetect@Collision@@QAE_NPAVObject@@0@Z)
1>Collision.obj : error LNK2019: unresolved external symbol "public: float __thiscall Object::getBottom(void)" (?getBottom@Object@@QAEMXZ) referenced in function "public: bool __thiscall Collision::CollisionDetect(class Object *,class Object *)" (?CollisionDetect@Collision@@QAE_NPAVObject@@0@Z)
1>Collision.obj : error LNK2019: unresolved external symbol "public: float __thiscall Object::getTop(void)" (?getTop@Object@@QAEMXZ) referenced in function "public: bool __thiscall Collision::CollisionDetect(class Object *,class Object *)" (?CollisionDetect@Collision@@QAE_NPAVObject@@0@Z)
1>C:\Users\Student\Dropbox\Work\Year 2\Graphics 1\Coursework\GraphicsCoursework\Debug\GraphicsCoursework.exe : fatal error LNK1120: 4 unresolved externals

是我的遗传问题导致的,还是其他原因?

最佳答案

准确的把对象类中的4个函数做成“virtual”事实上,我建议将其设为纯虚拟,

class Object{
public:
Object(void);
~Object(void);

virtual GLfloat getLeft() = 0;
virtual GLfloat getRight() = 0;
virtual GLfloat getBottom() = 0;
virtual GLfloat getTop() = 0;
};

这将迫使您在派生类中覆盖这四个函数,并提供一个定义..

关于c++ - 编译器错误 LKN2019 : Unresolved external symbol when dealing with inheritance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15016770/

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