Cannot allocate an object of abstract type ‘WallTile’|

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!


Japper28 wrote:
Finally, 'WallTile' and 'Ball' has either the getSquare or the getCircle function, returning a sf::CircleShape or sf:: RectangleShape:
You are misunderstanding virtual functions - to be able to instantiate an instance of a class, it must have no pure-virtual functions. You can't just implement the ones you want and leave the others alone - you have to implement all of them. I think you need to consider a redesign.
I somehow was expecting something like that.. Well that sucks. Any chance you could give me some small tips on a different approach?? Like in pseudo code? I would really appreciate!
Since sf::CircleShape and sf::RectangleShape both extend sf::Shape, why not combine the member functions into one? virtual sf::Shape getShape() = 0;

http://www.sfml-dev.org/documentation/2.0/classsf_1_1Shape.php
Last edited on
Thanks! How did I not know this before? I will try it.
Topic archived. No new replies allowed.