C++链接器错误LNK2019

C++ Linker Error LNK2019

本文关键字:LNK2019 错误 链接 C++      更新时间:2023-10-16

我还是C++的新手,不知道为什么在尝试调用另一个类中的这些函数时会出现这些链接器错误。

错误为:

error LNK2019: unresolved external symbol "public: float __thiscall Star::getMass(void)" (?getMass@Star@@QAEMXZ) referenced in function "public: void __thiscall Projectile::Update(class Star * const,int)" (?Update@Projectile@@QAEXQAVStar@@H@Z)
error LNK2019: unresolved external symbol "public: float __thiscall Star::getX(void)" (?getX@Star@@QAEMXZ) referenced in function "public: void __thiscall Projectile::Update(class Star * const,int)" (?Update@Projectile@@QAEXQAVStar@@H@Z)
error LNK2019: unresolved external symbol "public: float __thiscall Star::getY(void)" (?getY@Star@@QAEMXZ) referenced in function "public: void __thiscall Projectile::Update(class Star * const,int)" (?Update@Projectile@@QAEXQAVStar@@H@Z)

Project.cpp:

#include <hge.h>
#include "Projectile.h"
#include "Physics.h"
#include "Star.h"
#include <math.h>
Projectile::Projectile(float xV, float yV, float x, float y, float m, HTEXTURE tex)
{
    xVel = xV;
    yVel = yV;
    xPos = x;
    yPos = y;
    mass = m;
    quad.tex = tex;
}
void Projectile::Update(Star stars[], int length)
{
    for(int i = 0; i<length; ++i)
    {
        float force = Physics::calcGravityForce(mass, stars[i].getMass(), Physics::calcDist(xPos, yPos, stars[i].getX(), stars[i].getY()));
        Accelerate(force, stars[i].getX() - xPos, stars[i].getY() - yPos);
    }
}
void Projectile::Accelerate(float force, float x, float y)
{
    float c = sqrt((x * x) + (y * y));
    xVel += x/c;
    yVel += y/c;
}

Star在Star.h中定义如下:

#ifndef STAR_H
#define STAR_H
#include <hge.h>
class Star
{
private:
    float mass, radius, x, y;
    hgeQuad quad;
public:
    Star(float m, float r, float X, float Y, HTEXTURE);
    float getMass();
    float getRadius();
    float getX();
    float getY();
    Star() {}
};
#endif

Star类中声明了几个函数:

Star(float m, float r, float X, float Y, HTEXTURE);
float getMass();
float getRadius();
float getX();
float getY();

您试图在不提供定义的情况下使用其中一些,也就是说,函数的主体,这就是为什么您会遇到这些链接器错误的原因。

在名为Star.cpp的项目中添加一个新的.cpp文件(不过名称无关紧要(,并为Star类添加函数的定义,就像为Projectile类所做的那样。(您可以将它们添加到项目中的任何.cpp文件中,如Projectile.cpp,但如果您有一个单独的头文件,也可以有一个独立的.cpp文件。(

或者,如果您不想在项目中有另一个cpp文件,您可以将函数体放在类本身中:

class Star
{
private:
    float mass, radius, x, y;
    hgeQuad quad;
public:
    Star(float m, float r, float X, float Y, HTEXTURE);
    float getMass() { return mass; }
    float getRadius() { return radius; }
    float getX() { return x; }
    float getY() { return y; }
    Star() {}
};

这种风格对于getMassgetRadius等只返回成员变量的小型"getter"函数很常见。


虽然这与你的问题没有直接关系,但我应该指出几点:

  1. 将所有"getter"函数(如getMass等(设置为const(以便它们可以用于const Star对象(,方法是在参数(本例中为()(后面加上单词const,如下所示:float getMass() const { return mass; }

  2. 因为Star类中有成员变量,所以应该在不带参数的构造函数中将它们设置为一些合理的默认值

像这样:

Star() : mass(0), radius(0), x(0), y(0) {}

massradiusxy设置为0。(这种不寻常的语法被称为初始化程序列表。你可以在这里阅读它们。(

您甚至可以使用默认参数在没有单独构造函数的情况下完成此操作:

Star(float m = 0, float r = 0, float X = 0, float Y = 0, HTEXTURE = 0);