I’m to extend a C++ class, but have absolutely no background in that language. Googling didn’t help me understand how to solve the compilation error:
Constructor for 'JRB2World' must explicitly initialize the base class 'b2World' which does not have a default constructor
So, there is this b2World.h
class b2World
{
public:
b2World(const b2Vec2& gravity);
~b2World();
// ...
And its .cpp:
b2World::b2World(const b2Vec2& gravity)
{
// ...
}
b2World::~b2World()
{
// ...
}
My class header:
#import "Box2D.h"
class JRB2World : public b2World {
float factor;
public:
JRB2World(const b2Vec2& gravity);
~JRB2World();
float getFactor();
void setFactor(float f);
};
My class implementation:
JRB2World::JRB2World(const b2Vec2& gravity) {
// Constructor for 'JRB2World' must explicitly initialize the base class 'b2World' which does not have a default constructor
}
JRB2World::~JRB2World() {
}
float JRB2World::getFactor(){
return factor;
}
void JRB2World::setFactor(float f){
factor = f;
}
I suppose it has to do with a call to the “super constructor” like in java or objc. How can this be done?
Just put the constructor into the initializer list of the inherited class’ constructor:
Depending on what you want to do, I wouldn’t use inheritance here: Put the Box2D world into a member variable. That should make it easier to replace it, just in case its interfaces changes over Versions or you decide to use another physics engine later on.