I'm struggling with this problem for some days now and can't seem to find any solutions on the web. I'm trying to learn how to use virtual functions properly, but without succes...
I get this error on my derived 'WallTile' class
error: cannot allocate an object of abstract type ‘Ball’|
note: because the following virtual functions are pure within ‘Ball’:|
note: virtual sf::RectangleShape Tile::getSquare()|
|
And also the same error for the derived 'Ball' Class.
I have this base class called 'Tile', and 2 derived classes 'WallTile' and 'Ball'.
These 2 functions creates a new ball or wall:
1 2
|
sprite->addEmptyRectangleObject(new WallTile(0, 0, sf::Color::Black, "wall"));
sprite->addColoredBallObject(new Ball(posY, posX, 0, sf::Color::Red, "Ball"));
|
in 'Sprite' it actually creates a new instance of the class:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
void Sprite::addEmptyRectangleObject(Tile * _tile)
{
assert(_tile != NULL);
tile = _tile;
squareTiles.push_back(tile->getSquare());
}
void Sprite::addColoredBallObject(Tile * _tile)
{
assert(_tile != NULL);
tile = _tile;
balls.push_back(tile->getCircle());
}
|
'Tile' holds 2 virtual functions:
1 2
|
virtual sf::RectangleShape getSquare() =0;
virtual sf::CircleShape getCircle() =0;
|
Finally, 'WallTile' and 'Ball' has either the getSquare or the getCircle function, returning a sf::CircleShape or sf:: RectangleShape:
Header (in this case WallTile):
virtual sf::RectangleShape getSquare();
Implementation:
1 2 3 4 5
|
sf::RectangleShape WallTile::getSquare()
{
std::cout << "New wall added to stage at position " << x << "," << y << std::endl;
return obj;
}
|
What causes this problem, and is this the right structure to achieve my goal?
Thanks in advance!