c++undefined-reference

undefined reference compiler error in c++


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.


Solution

  • 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