Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Inheritance hierarchy problem. 'cannot instantiate abstract class'  (Read 5996 times)

0 Members and 1 Guest are viewing this topic.

Noob

  • Newbie
  • *
  • Posts: 15
    • View Profile
Greetings.

So I'm a bit of a C++ noob and using SFML 2.1 to make a little game project to get better at programming.

I'm having problems when creating an inheritance hierarchy for the entities I plan on using for my game.
I made an Entity class in a separate header to use as a base class to derive other stuff like a player class and enemy class from.

What would the best way of having it hold a 'draw' function that the derived classes can use to draw themselves to the RenderWindow that I have in my main() be?
The tutorial on 'Creating a SFML-like entity' was confusing, and the example code gives me 'object of abstract class type "Entity" is not allowed;' errors.

I'm not going to be using vertex arrays. Just simple shapes (rectangles, circles, sprites) for now.

Quote
Entity.h

class MyEntity : public sf::Drawable, public sf::Transformable
{
public:
   //   Some stuff

private:

   //   pure virtual function
    virtual void drawStuff(sf::RenderTarget& target, sf::RenderStates states) const = 0;


};

Quote
Player.h
#include "Entity.h"

class Player : public MyEntity
{
public:
   // Player stuff

private:

   void drawStuff(sf::RenderTarget& target, sf::RenderStates states) const
    {
        states.transform *= getTransform();

        target.draw(playerIcon, states);
    };

int playerXPosition;
int playerYPosition;
// Rectangle to represent player (placeholder)
sf::RectangleShape playerIcon;

};

Quote
Output
1>Game.cpp(22): error C2259: 'MyEntity' : cannot instantiate abstract class
1>          due to following members:
1>          'void sf::Drawable::draw(sf::RenderTarget &,sf::RenderStates) const' : is abstract
1>          C:\Users\User\Documents\Programming\SFML-2.1-windows-vc10-32bits\SFML-2.1\include\SFML/Graphics/Drawable.hpp(69) : see declaration of 'sf::Drawable::draw'
1>          'void MyEntity::drawStuff(sf::RenderTarget &,sf::RenderStates) const' : is abstract
1>          c:\users\user\documents\programming\crappygame\crappygame\Entity.h(13) : see declaration of 'MyEntity::drawStuff'
1>Game.cpp(23): error C2259: 'Player' : cannot instantiate abstract class
1>          due to following members:
1>          'void sf::Drawable::draw(sf::RenderTarget &,sf::RenderStates) const' : is abstract
1>          C:\Users\User\Documents\Programming\SFML-2.1-windows-vc10-32bits\SFML-2.1\include\SFML/Graphics/Drawable.hpp(69) : see declaration of 'sf::Drawable::draw'

Why can't I instantiate an abstract class?

To my knowledge the pure virtual drawStuff function is what makes Entity abstract, but I've tried removing it and still get the errors.

Also, what is the 'sf::RenderStates states' parameter meant to do and how would 'states' be passed in as an argument when calling the function?? I've looked at the documentation but it didn't make any sense to me.

Also, I tried searching the forums but the search thing appears broken. I tried it a few times and it told me to inform an admin.

I know I've made a rat's ass of this whole thing but please don't hate me. :(

Windows 7, Visual C++ 2010, Release config.
Killing goblins at Lumbridge. Windows 7 - 64 bit. AMD Radeon HD 6700. Using VS2012 Express, SFML 2.1 - Static.

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: Inheritance hierarchy problem. 'cannot instantiate abstract class'
« Reply #1 on: January 31, 2014, 11:33:08 pm »
As the error message says, sf::Drawable has a pure virtual method named draw. You need to implement it in classes which inherit from sf::Drawable or they will be abstract too and you won't be able to instantiate them.

AncientGrief

  • Newbie
  • *
  • Posts: 32
    • Yahoo Instant Messenger - what?
    • View Profile
Re: Inheritance hierarchy problem. 'cannot instantiate abstract class'
« Reply #2 on: January 31, 2014, 11:48:55 pm »
http://www.sfml-dev.org/documentation/2.0/classsf_1_1Drawable.php

sf:Drawable declares the method this way:
virtual void draw (RenderTarget &target, RenderStates states) const =0
 

const = 0 means you MUST implement this method in your deriving class if you want to create an instance!

But you made a small mistake:
void drawStuff(sf::RenderTarget& target, sf::RenderStates states) const
 

you named your method drawStuff => change it to draw.
Also: delete the drawStuff method from MyEntity if you don't plan to create an instance of it, otherwise you MUST include a drawStuff method aswell in your Player class. This wouldn't make sense.

TL;DR:
Implement all pure virtual methods in a derived class, if you want to create an instance of it!
« Last Edit: January 31, 2014, 11:52:10 pm by AncientGrief »
I grew up with C++...but then I met C#...got lazy...time to turn back to my one and only love!
My system:
Wintendo 10 with Intel i7 4770k
Palit GeForce GTX 980
16 GB RAM