undefined reference compiler error in c++
I am getting the error message below when I try to compile开发者_Go百科 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.
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.
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
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();
.
精彩评论