0

this使用关键字时出现链接器错误:

Error 1 error LNK2001: unresolved external symbol "public: virtual void __thiscall GameObject::update(void)" (?update@GameObject@@UAEXXZ) Main.obj Pong C++ Conversion

这是代码

class GOBall: public GameObject
{
public:
    static const GLint SIZE;
    static const GLfloat MAX_SPEEDX;
    static const GLfloat MAX_SPEEDY;
    static const GLfloat DAMPING;
    GLfloat velX;
    GLfloat velY;
    GLfloat startX;
    GLfloat startY;
    GOBall(GLfloat x, GLfloat y);
    void update();
    void reverseX(GLfloat center);
    void reverseY();
    void resetPosition();
};
const GLfloat GOBall::DAMPING = 0.05f;
const GLfloat GOBall::MAX_SPEEDX = 4;
const GLfloat GOBall::MAX_SPEEDY = 8;
const GLint GOBall::SIZE = 16;
GOBall::GOBall(GLfloat x, GLfloat y)
{
    this->x = x;//The Error appeared after filling in this function
    this->y = y;

    this->sx = SIZE;
    this->sy = SIZE;
    startX = x;
    startY = y;
    velX = -MAX_SPEEDX;
    velY = 0;
}

x 变量在 GameObject 类中

class GameObject
 {
protected:
    GLfloat x, y,sx, sy;
public:
    virtual void update();
    void render();
    GLfloat getX();
    GLfloat getY();
    GLfloat getSX();
    GLfloat getSY();
    GLfloat getCenterY();
};

有些人可能会注意到,我一直在尝试通过这些教程重新创建 Java 应用程序 Pong,以便更好地了解 OpenGL 和 C++ https://www.youtube.com/playlist?list=PL513808FE7D9A5D68

而且我知道在 header/cpp 文件中实现这个游戏可能更容易,但我对首先包含哪个类头感到困惑,因为有四个GameObject类,并且它们的变量在它们的实例之间到处乱飞

4

1 回答 1

0

该错误通常意味着您忘记实现一个函数(或错过了一个库),并且通常只在需要时出现。

在这种情况下,这意味着方法GameObject::update()updateroot 的方法GameObject)没有实现。如果它是抽象的,那么=0在声明后面添加一个:

class GameObject
 {
protected:
    GLfloat x, y,sx, sy;
public:
    virtual void update()=0;
    void render();
    GLfloat getX();
    GLfloat getY();
    GLfloat getSX();
    GLfloat getSY();
    GLfloat getCenterY();
};
于 2014-05-12T08:29:03.387 回答