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)
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() {}
};
这种风格对于getMass
、getRadius
等只返回成员变量的小型"getter"函数很常见。
虽然这与你的问题没有直接关系,但我应该指出几点:
将所有"getter"函数(如
getMass
等(设置为const
(以便它们可以用于const Star
对象(,方法是在参数(本例中为()
(后面加上单词const
,如下所示:float getMass() const { return mass; }
因为
Star
类中有成员变量,所以应该在不带参数的构造函数中将它们设置为一些合理的默认值
像这样:
Star() : mass(0), radius(0), x(0), y(0) {}
将mass
、radius
、x
和y
设置为0
。(这种不寻常的语法被称为初始化程序列表。你可以在这里阅读它们。(
您甚至可以使用默认参数在没有单独构造函数的情况下完成此操作:
Star(float m = 0, float r = 0, float X = 0, float Y = 0, HTEXTURE = 0);
相关文章:
- 链接CPP文件进行测试时发生LNK2019错误
- 32位到64位:LNK2019错误,如何检索相关信息
- 在结构函数之间传递文件路径 C++ 编辑:修复LNK2019错误
- 尝试使用 TMXlite 编译项目时LNK2019错误
- LNK2019错误;此处缺少/未定义的内容
- DX11 DirectInput8创建引起LNK2019错误
- C++ mysql 仅在构建解决方案时LNK2019错误
- C LNK2019错误,未解决的外部字符
- C++ 带有出现LNK2019错误的对象的链接列表
- 解决 main 中的LNK2019错误
- 尝试使用命名空间中的函数时LNK2019错误
- LNK2019错误未解析的外部符号
- LNK2019错误,在Visual Studio 2012 C++的main中使用的函数
- 同一项目中两个类之间的visual c++LNK2019错误
- LNK2019错误(未解析的外部符号)将SQLite链接到DLL
- C++/CLI 包装器LNK2028和LNK2019错误
- LNK2019错误,尝试覆盖运算符<< 对于 LinkedList 类
- LNK2001和LNK2019错误 - DirectX 未解析的外部符号
- (2012)Visual C++LNK2019错误,可能是模板问题
- LNK2019错误一个DLL链接到MFC DLL