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

Object.h:

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

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

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

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

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

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

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

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

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

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

Collision.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将比较两个不同的对象是否发生碰撞,为此,我需要特定的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个功能设为“虚拟”
实际上,我建议将其设为纯虚拟,

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

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

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

10-06 10:00