c++ 中未定义的引用编译器错误
当我尝试编译代码时收到以下错误消息 -
在函数中 '__static_initialization_and_destruction_0': home/user/main.cpp:50: 未定义 参考 'PhysEng2D::PhysEng2D(void)'
第 50 行唯一的代码是 -
PhysEng2D Physics;
PhysEng2D 的头文件是 -
#ifndef _PHYSENG2D_H_
#define _PHYSENG2D_H_
#include "primitives.h"
class PhysEng2D
{
public:
PhysEng2D::PhysEng2D();
PhysEng2D::~PhysEng2D();
bool IsBoundingBoxCollision(PS2Sprite & S1, PS2Sprite & S2);
bool IsWallCollision(PS2Sprite & S);
};
#endif
PhysEng2D 其余部分的开头是 -
#include "primitives.h"
#include "physeng2d.h"
PhysEng2D::PhysEng2D()
{
//Nothing to Initialise
}
PhysEng2D::~PhysEng2D()
{
//Nothing to clean up
}
(我没有完整包含这些方法,因为我不认为它们是相关的)
抱歉,我知道这可能是我犯的一个非常愚蠢的小错误。
I am getting the error message below when I try to compile my code -
In function
'__static_initialization_and_destruction_0':
home/user/main.cpp:50: undefined
reference to
'PhysEng2D::PhysEng2D(void)'
The only code on line 50 is -
PhysEng2D Physics;
The header file for PhysEng2D is -
#ifndef _PHYSENG2D_H_
#define _PHYSENG2D_H_
#include "primitives.h"
class PhysEng2D
{
public:
PhysEng2D::PhysEng2D();
PhysEng2D::~PhysEng2D();
bool IsBoundingBoxCollision(PS2Sprite & S1, PS2Sprite & S2);
bool IsWallCollision(PS2Sprite & S);
};
#endif
And the beginning of the rest of PhysEng2D is -
#include "primitives.h"
#include "physeng2d.h"
PhysEng2D::PhysEng2D()
{
//Nothing to Initialise
}
PhysEng2D::~PhysEng2D()
{
//Nothing to clean up
}
(I didn't include the methods in full because I didn't think they were relevant)
Sorry, I am aware that this is probably a very stupid little error that I'm making.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
头文件中的构造函数和析构函数不应包含类的名称。
更改
为
并且您不需要在 .cpp 中重新包含“primitives.h”。
Your constructor and destructor in the header file should not contain the name of the class.
Change
To
And you don't need to reinclude "primitives.h" in the .cpp.
您需要编译每个cpp文件,然后链接它们。
您还应该从 .h 中的类定义中删除
PhysEng2D::
前缀You need to compile each cpp file, then link them.
You also should remove the
PhysEng2D::
prefix from the class definition in the .h您似乎忘记将
PhysEng2D.o
与main.o
链接。另外,PhysEng2D::PhysEng2D();
语法在类定义中无效:它应该只是说PhysEng2D();
。It looks like you forgot to link
PhysEng2D.o
withmain.o
. AlsoPhysEng2D::PhysEng2D();
syntax isn't valid inside the class definition: It should just sayPhysEng2D();
.