Reputation: 447
Hello all I though it would be fun exercise to try to make a Sierpinski Triangle and I am having an issue with this abstract class error that I can't seem to figure out. When I try to make a SierpinskiTri "SierpinskiTri sierpinskiii;" object and then draw it to the window "window.draw(sierpinskiii);" i get an error on that object saying it is an abstract class
class SierpinskiTri : public sf::Drawable{
public :
SierpinskiTri(sf::RenderWindow &window):m_window(window) {
//filledTriangle(300, 400, 300);
sierpinski(3 ,300, 0,300);
}
virtual void sierpinski(int n, double x, double y, double s){
filledTriangle(x, y, s);
sierpinski(n-1, x - s/2, y, s/2);
sierpinski(n-1, x + s/2, y, s/2);
sierpinski(n-1, x, y+sqrt(3)/2*s, s/2);
if(n == 0)
{
return;
}
}
virtual void filledTriangle(float x, float y, float s){
float* xl = new float[4];
xl[0] = x - s/2; xl[1]= x + s/2; xl[2] = x; xl[3] = x-s/2;
float* yl = new float[4];
yl[0] = sqrt(3)/2*s+y; yl[1] = yl[0]; yl[2] = y; yl[3] = yl[0];
draw(xl,yl,m_window);
}
private:
sf::RenderWindow &m_window;
virtual void draw(float xl[],float yl[],sf::RenderWindow &window) const {
sf::VertexArray triangle(sf::Triangles, 3);
triangle[0].position = sf::Vector2f(xl[0],yl[1]);
triangle[0].color = sf::Color::Blue;
triangle[1].position = sf::Vector2f(xl[1],yl[1]);
triangle[1].color = sf::Color::Red;
triangle[2].position = sf::Vector2f(xl[2],xl[2]);
triangle[2].color = sf::Color::White;
window.draw(triangle);
}
};
Upvotes: 1
Views: 486
Reputation: 11011
By documentation you have to override that function:
virtual void sf::Drawable::draw( RenderTarget& target
, RenderStates states ) const;
You have written that function:
virtual void draw( float xl[]
, float yl[]
, sf::RenderWindow &window ) const;
As result your derived class is abstract like compiler tells. In order to override you need to match the signature of virtual function correctly.
Upvotes: 2
Reputation: 303517
When you inherit from Drawable
:
class SierpinskiTri : public sf::Drawable { .. };
you also inherit all of its methods, one of which is:
virtual void draw (RenderTarget &target, RenderStates states) const = 0;
You are not providing an implementation of draw
, so your SierpinksiTri
class is also an abstract class and you cannot create an instance of an abstract class.
The solution is to provide an implementation for draw()
.
Upvotes: 3