C++ 链接器错误 LNK2019

标签 c++ linker lnk2019

我对 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)

射弹.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();

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

将一个新的 .cpp 文件添加到名为 Star.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 上使用objects),方法是在参数(本例中为 ())后面加上单词 const,如下所示: float getMass() const { return mass; }

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

像这样:

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

这会将 massradiusxy 设置为 0 . (这种不寻常的语法称为初始化程序列表。您可以阅读有关它们的信息 here。)

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

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

关于C++ 链接器错误 LNK2019,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7295252/

相关文章:

c++ - 为什么我的功能不算数?

c - _aenvptr 和 _wenvptr 的重复定义

c - 将从源代码构建的库链接到由 autotools 管理的程序

c++ - 问题 : "error LNK2019: unresolved external symbol"

c++ - -2[array] 是什么意思

c++ - 从 WM_CHAR 消息中获取虚拟键码

c++ - 尝试在树莓派上部署 qt5 应用程序时出现奇怪的错误

iphone - iPhone 设备 3.0 静态库中的类别

c++ - 使用 ALUT 时出现错误 LNK2019

c++ - SOIL 未解析的外部符号