Function not found from derived class?

Hi!

I have two classes where the one is derived from the other - As you probably can see below:

class LevelObject : public Node
{
private:
	// It helps to typedef super & self so if you change the name
	// of the class or super class, you don't have to replace all
	// the references
	typedef Node super;
	typedef LevelObject self;

protected:
	b2Body* body;
	Sprite* sprite;

public:
	LevelObject();
	void addBodyToWorld(b2World* world);
	void addCircularFixtureToBody(float radius);
	void addRectangularFixtureToBody(float width, float height);
	void createFixture(b2Shape* shape);
	void setProperties(ValueMap& properties);
	virtual ~LevelObject();
};
class Player : public LevelObject
{
private:


protected:
	b2Body* body;
	Sprite* sprite;
	b2World* world;
public:
	void addBodyToWorld(b2World* world);
	void addFixturesToBody();

};

As you can see the player is derived from LevelObject.
But then later on I make a function like:

LevelObject* Level::addObject(string className, ValueMap& properties)
{
	// create the object
	LevelObject* o = nullptr;
	if (className == "Player")
		o = new Player;

		//o = new MagicChest;

	// process the new object
	if (o != nullptr)
	{
		o->setProperties(properties);
		o->addBodyToWorld(this->world);
		o->addFixturesToBody();
		this->addChild(o);
	}
	return o;
}

Where I get an error that says: “LevelObject has no member named AddFixturesToBody()” but why?
I mean shouldn’t it be able to find the function created in the derived class or what should I do?
Btw, all tips are appreciated very much!

Thank you

Because o is of type LevelObject, and LevelObject doesn’t have a addFixturesToBody function.

But doesn’t the parent classes also have access to the derived or is it just the derived classes that have access to the parent? And can I do something from the derived class so that the levelObject can access the addFixturesToBody() without adding it or is that what I have to do?

THANK YOU SO MUCH!

Only derived classes have access to parent classes. You can add addFixturesToBody as a virtual function to your parent class, or you can cast your object to your derived class: (Player*)o->addBodyToWorld(this->world);

1 Like