0
\$\begingroup\$

What i want to do is make the Player class inherit the Sprite class.

So I inherited it like this :

class Player: public Sprite {};

But when I go to my Player.cpp I get the following error :

Error : No default constructor exists for "Sprite".

But it does have a constructor (well, 2 of them...) :

Sprite(int index, sf::Vector2f position);
Sprite(int index, sf::Vector2f position, float rotation);

Also, not sure if this is the right place to post this. But since it contains SFML library code I will post it here.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

This is a pure c++ error and in the future it should be asked on SO, but I'll answer at the end of the post.


This said, with OOP, I would suggest you have your player use a sprite, and not be a sprite.

A player is not a sprite, a player has a sprite. Making this kind of architectural choice early in the development can get you in a big mess.


Since the parent class does not have a default constructor, your children class cannot use a default compiler-generated constructor as it will not be able to call the default constructor of the parent (because it does not have any).

To fix this issue, you have to explicitly add a constructor to your Player class and call the constructor for the parent class (the Sprite).

class parent
{
public:
  parent( int p1, float p2 ) {}
};

// This will not compile
class child : public parent
{
public:
};

// This will compile
class child2 : public parent
{
public:
  child2() : parent( 1, 2.9f ) {}
};
\$\endgroup\$
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.