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

Author Topic: Inheritance sf::Drawable  (Read 4391 times)

0 Members and 1 Guest are viewing this topic.

Poraft

  • Newbie
  • *
  • Posts: 24
    • View Profile
Inheritance sf::Drawable
« on: July 05, 2011, 01:08:04 pm »
Hello,

I'm making a menu class, that inherits from sf::Drawable. So that I can simply draw it.
Menu.h
Code: [Select]
#ifndef _MENU_H_
#define _MENU_H_

#include <SFML/Graphics.hpp>

class menu : public sf::Drawable
{
private:
sf::Image buttonImage;
sf::Image menuBackgroundImage;
sf::Sprite buttonSprite;
sf::Sprite menuBackgroundSprite;


public:
//constructor
menu();
void pauseMenu();

};
#endif


And in main I just make an instance of a menu, then I get this error;

Error:
Code: [Select]
main.cpp: In function ‘int main()’:
main.cpp:8:8: error: cannot declare variable ‘myMenu’ to be of abstract type ‘menu’
menu.h:7:1: note:   because the following virtual functions are pure within ‘menu’:
/usr/include/SFML/Graphics/Drawable.hpp:503:18: note: virtual void sf::Drawable::Render(sf::RenderTarget&, sf::Renderer&) const


Can you give me some help, or tell me another way how I should make the menu? Maybe a button class or something?

Thanks, and sorry for my bad english;]

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Inheritance sf::Drawable
« Reply #1 on: July 05, 2011, 01:34:43 pm »
Quote from: "Poraft"
I'm making a menu class, that inherits from sf::Drawable. So that I can simply draw it.
I wouldn't do it like this. Inheritance has some drawbacks, even if it seems to be simple in the first term. By deriving your class from sf::Drawable, you state that your menu contains all of the properties of a sf::Drawable object. Do things like SetBlendMode(), SetRotation() etc. really make sense for your menu?

Why don't you do it like this?
Code: [Select]
class Menu
{
    public:
        Menu();

        void Draw(sf::RenderWindow& window) const
        {
            window.Draw(menuBackgroundSprite);
            window.Draw(buttonSprite);
        }
};

By the way, identifiers containing double underscores or starting with an underscore are reserved for the standard library and compiler implementation. Rename _MENU_H_ to MENU_H or something else.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Inheritance sf::Drawable
« Reply #2 on: July 05, 2011, 01:43:37 pm »
Also why it fails for you to compile is because sf::Drawable is an abstract class so you have to define it's abstract functions in the derived class. If you do not understand a thing I am saying then you have to learn C++, preferably from a teacher or book.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

JAssange

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Re: Inheritance sf::Drawable
« Reply #3 on: July 05, 2011, 01:43:54 pm »
Quote from: "Nexus"
By the way, identifiers containing double underscores or starting with an underscore are reserved for the standard library and compiler implementation. Rename _MENU_H_ to MENU_H or something else.

Or he could just use #pragma once which takes less code and is supported by every modern compiler I've ever used.

Poraft

  • Newbie
  • *
  • Posts: 24
    • View Profile
Inheritance sf::Drawable
« Reply #4 on: July 05, 2011, 01:53:58 pm »
Thanks for the help!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Inheritance sf::Drawable
« Reply #5 on: July 05, 2011, 02:02:36 pm »
[Offtopic]

JAssange, please quote next time only the relevant part. The thread stays clean and people immediately see what you refer to ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

JAssange

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Inheritance sf::Drawable
« Reply #6 on: July 05, 2011, 05:11:01 pm »
Quote from: "Nexus"
[Offtopic]

JAssange, please quote next time only the relevant part. The thread stays clean and people immediately see what you refer to ;)

I usually do, missed it there.

Poraft

  • Newbie
  • *
  • Posts: 24
    • View Profile
Inheritance sf::Drawable
« Reply #7 on: July 07, 2011, 11:15:31 pm »
Sorry, I fixed my problem already..

 

anything