c++ 中未定义的引用编译器错误

发布于 2024-11-05 19:14:15 字数 880 浏览 2 评论 0原文

当我尝试编译代码时收到以下错误消息 -

在函数中 '__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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

优雅的叶子 2024-11-12 19:14:15

头文件中的构造函数和析构函数不应包含类的名称。

更改

PhysEng2D::PhysEng2D(); 
PhysEng2D::~PhysEng2D();

PhysEndg2D();
~PhysEng2D();

并且您不需要在 .cpp 中重新包含“primitives.h”。

Your constructor and destructor in the header file should not contain the name of the class.

Change

PhysEng2D::PhysEng2D(); 
PhysEng2D::~PhysEng2D();

To

PhysEndg2D();
~PhysEng2D();

And you don't need to reinclude "primitives.h" in the .cpp.

在梵高的星空下 2024-11-12 19:14:15

您需要编译每个cpp文件,然后链接它们。

g++ -c -Wall main.cpp
g++ -c -Wall physeng2d.cpp

g++ -o myapp main.o physeng2d.o

您还应该从 .h 中的类定义中删除 PhysEng2D:: 前缀

You need to compile each cpp file, then link them.

g++ -c -Wall main.cpp
g++ -c -Wall physeng2d.cpp

g++ -o myapp main.o physeng2d.o

You also should remove the PhysEng2D:: prefix from the class definition in the .h

提赋 2024-11-12 19:14:15

您似乎忘记将 PhysEng2D.omain.o 链接。另外,PhysEng2D::PhysEng2D();语法在类定义中无效:它应该只是说PhysEng2D();

It looks like you forgot to link PhysEng2D.o with main.o. Also PhysEng2D::PhysEng2D(); syntax isn't valid inside the class definition: It should just say PhysEng2D();.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文