SFML community forums
Help => Graphics => Topic started 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
#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:
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;]
-
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?
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.
-
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.
-
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.
-
Thanks for the help!
-
[Offtopic]
JAssange, please quote next time only the relevant part. The thread stays clean and people immediately see what you refer to ;)
-
[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.
-
Sorry, I fixed my problem already..