繁体   English   中英

C ++链接器错误LNK2019

[英]C++ Linker Error 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)

Projectile.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() {}
};

这种样式对于小的“ getter”函数(例如getMassgetRadius等)很常见,这些函数仅返回成员变量。


尽管它与您的问题没有直接关系,但我应该指出一些事情:

  1. 通过将const字样放在参数(在本例中为()之后,使所有“ getter”函数(如getMass等)成为const (以便可以在const Star对象上使用),如下所示: float getMass() const { return mass; } float getMass() const { return mass; }

  2. 由于Star类中有成员变量,因此应在不带参数的构造函数中将其设置为合理的默认值,

像这样:

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

它将massradiusxy0 (这种不寻常的语法称为初始化程序列表 。您可以在此处阅读有关它们的信息 。)

您甚至可以通过使用默认参数而无需单独的构造函数来执行此操作:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM