SFML community forums

Help => Graphics => Topic started by: Poraft on July 05, 2011, 01:08:04 pm

Title: Inheritance sf::Drawable
Post by: Poraft 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;]
Title: Re: Inheritance sf::Drawable
Post by: Nexus 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.
Title: Inheritance sf::Drawable
Post by: Groogy 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.
Title: Re: Inheritance sf::Drawable
Post by: JAssange 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.
Title: Inheritance sf::Drawable
Post by: Poraft on July 05, 2011, 01:53:58 pm
Thanks for the help!
Title: Inheritance sf::Drawable
Post by: Nexus 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 ;)
Title: Inheritance sf::Drawable
Post by: JAssange 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.
Title: Inheritance sf::Drawable
Post by: Poraft on July 07, 2011, 11:15:31 pm
Sorry, I fixed my problem already..