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

Author Topic: Can't inherit sf::Drawable  (Read 2871 times)

0 Members and 1 Guest are viewing this topic.

PhoenixSuzumiya

  • Newbie
  • *
  • Posts: 2
    • View Profile
Can't inherit sf::Drawable
« on: July 10, 2013, 06:19:17 pm »
Hi everyone, this is the first time I post on this forum and I need a little help.
I've been messing around with SFML for a month now and I wanted my entities to inherit the draw function from sf::Drawable (as seen on the Vertex Array tutorial page) ò.ò
Here's a minimal example of the class:
Entity.h
#include <SFML/Graphics.hpp>

#ifndef ENTITY_H
#define ENTITY_H

class Entity : public sf::Drawable
{
    public:
    Entity();
    sf::Texture Texture;
    sf::Sprite Sprite;
    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
};

#endif
 

Entity.cpp
#include "Entity.h"

Entity::Entity()
{
    Texture.loadFromFile("Flashlight.png");
    Sprite.setTexture(Texture);
}
 

The error I get:
undefined reference to `vtable for Entity'
 

My IDE is Code::Blocks and the SFML libreries are dinamically linked.
Thanks for you attention ^^

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Can't inherit sf::Drawable
« Reply #1 on: July 10, 2013, 06:54:19 pm »
You don't implement the draw function.
Laurent Gomila - SFML developer

PhoenixSuzumiya

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Can't inherit sf::Drawable
« Reply #2 on: July 10, 2013, 09:03:47 pm »
*facepalm*
It's the first time I try to inherit classes, I didn't know it needed to be implemented D:
Thank you! xD

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Can't inherit sf::Drawable
« Reply #3 on: July 10, 2013, 10:50:34 pm »
Well... just do like in the tutorial :P
Laurent Gomila - SFML developer

cpolymeris

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
    • Email
Re: Can't inherit sf::Drawable
« Reply #4 on: July 11, 2013, 09:40:23 am »
undefined reference to `vtable for Entity'
 

Truth be told, that's a horribly criptic error message. And I thought gcc was bad.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Can't inherit sf::Drawable
« Reply #5 on: July 11, 2013, 09:46:49 am »
Quote
Truth be told, that's a horribly criptic error message.
Agreed. This one is the worst.
Laurent Gomila - SFML developer

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Can't inherit sf::Drawable
« Reply #6 on: July 11, 2013, 05:16:12 pm »
Well... to be honest, if you understand what the error is telling you, it can be treated just as every other error. All that is needed is a bit of knowledge about how virtual methods are implemented in GCC and possibly other compilers.

When you override a virtual method in a polymorphic class, it should update the vtable for that class to point to the overridden version of the method. Since there is no definition for the method, the entry is never written and I guess since no entries are written GCC doesn't produce a vtable at all for the class, hence the "undefined reference to vtable" the linker complains about. The question I ask myself is: what happens if you don't provide definitions for all virtual methods, but only for some? Does it produce the same error message? Because in that case the error message wouldn't reflect the true cause of the error, and in this case, I agree that it can get rather cryptic :).
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

 

anything