Reputation: 21
(I'm pretty new to C++, so hopefully this is just a rookie mistake)
I'm having problems in my code, where I have a class "Player" that needs a number of attributes, which I'm try to give it though the use of abstract classes as such:
//player.h
class Player : public IUpdate, public IPositionable, public IMoveable, public IDrawable
{
public:
Player(void);
SDL_Rect get_position();
void move(Uint32 dTime);
void update(Uint32 dTime);
void show(SDL_Surface* destination);
~Player(void);
private:
SDL_Surface texture;
int x, y;
};
And I'm overriding the pure virtual functions as such:
//Player.cpp
Player::Player(void)
{
}
SDL_Rect Player::get_position()
{
SDL_Rect rect;
rect.h = 0;
return rect;
}
void Player::move(Uint32 dTime)
{
}
void Player::update(Uint32 dTime)
{
move(dTime);
}
void Player::show(SDL_Surface* destination)
{
apply_surface(x, y, &texture, destination, NULL);
}
Player::~Player(void)
{
}
However I keep getting the compilation error: C2259: 'Player' : cannot instantiate abstract class
As far as I can see, the pure virtual functions should be overridden, which my Google-searches have told me, would have made Player non-abstract, yet Player still appears to be abstract.
EDIT: The pure virtual functions:
class IPositionable
{
public:
virtual SDL_Rect get_position() = 0;
private:
int posX, posY;
};
class IUpdate
{
public:
virtual void update (Uint32 dTime) = 0;
};
class IMoveable
{
public:
int velX, velY;
virtual void move(Uint32 dTime) = 0;
};
class IDrawable
{
public:
virtual void show() = 0;
private:
SDL_Surface texture;
};
class IHitbox
{
virtual void check_collsion() = 0;
};
class IAnimated
{
virtual void next_frame() = 0;
int state, frame;
int rows, columns;
};
Upvotes: 2
Views: 7932
Reputation: 206518
Your problem is here:
class IDrawable
{
public:
virtual void show() = 0;
};
void Player::show(SDL_Surface* destination)
{
apply_surface(x, y, &texture, destination, NULL);
}
Note that Player::show(SDL_Surface* destination)
does not override the pure virtual method IDrawable::show()
.
In order to override the method you need to exact same function signature in derived class(only co-variant return types are allowed)
What you have right now is a method named show()
in derived class which hides the method named show()
in Base class it does not override it. Since you don;t provide definitions for all pure virtual functions of your class Player
compiler rightly tells you it is an Abstract class.
Upvotes: 4
Reputation: 6771
Surely it is caused by missing an override of a pure virtual function - maybe just a subtle sigature difference.
I would expect the compiler would tell you which function is still not overriden, something like (vc9):
C2259: 'Player' : cannot instantiate abstract class
due to following members:
'void IUpdate::update(void)' : is abstract
virtualclass.cpp(3) : see declaration of 'IUpdate::update'
If your compiler didn't report this, you could check by removing the inherited interface one by one.
Upvotes: 0
Reputation: 35449
It's possible that instead of overriding a pure virtual function of one of the bases, you've instead declared and defined a function with a subtly different signature, as in the following:
struct base {
virtual void foo(double d) = 0;
};
struct derived: base {
// does not override base::foo; possible subtle error
void foo(int i);
}
You might want to double-check your code by reviewing it. If you were using C++11, you'd be able to mark your functions override
to catch such errors.
Upvotes: 1
Reputation: 10052
In C++ a function is not virtual unless it is specifically written:
virtual void move(Uint32 dTime);
A pure virtual function is defined like this:
virtual void move(Uint32 dTime) = 0;
The "interfaces" you inherit from (notice that this is multiple inheritance.. C++ doesn't differ interfaces from classes) have pure virtual functions you didn't implement thus making your class abstract.
Upvotes: 0
Reputation: 59997
An abstract class is abstract - i.e. something is not defined but just declared.
You need to define all those methods. As I do not have the declarations for those classes I cannot advise you on what methods you are missing.
Upvotes: 0