Help with simple class definition using SFML 2.0

closed account (ypfz3TCk)
I'm trying to write a simple class that represents a missile (for a game). It uses SFML 2.0 and the class is basically a sprite type. Now the only interface function that I want at the moment is the call to 'move'. This is a SFML function that is a member of the Sprite class. So my class inherits from sf::Sprite and sf::Texture.

This is what I have done in the file missile.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef MISSILE_H
#define MISSILE_H

#include<SFML/Graphics.hpp>
#include<SFML/System.hpp>

class missile : public sf::Sprite, sf::Texture
{
 // member functions - perform operations on data members
 public:
  void move(float offsetX, float offsetY);

 private:
  // encapsulated data members
  static sf::Texture rocket_picture.loadFromFile("/home/neil/game/data/missile.png"); 
 missile.setTexture(rocket_picture); 
 missile.setScale(0.01f,0.02f); 
};


When i try to compile it i am getting these errors:
missile.h:15:22: error: expected ‘;’ at end of member declaration
missile.h:15:36: error: expected unqualified-id before ‘.’ token
missile.h:17:9: error: expected unqualified-id before ‘.’ token
missile.h:19:9: error: expected unqualified-id before ‘.’ token
-------

I have tried making some changes to the data members. All the data does is setup the sprite. I tried copying the code i used in main to setup the sprite's image (that works) into the class - but it did not compile.

Basically i want an instance of this class to have the same image! I know it is simple but i am having trouble as i have not used this SFML before. Can anyone help me with this please?
Last edited on
closed account (ypfz3TCk)
I have changed it to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef MISSILE_H
#define MISSILE_H

#include<SFML/Graphics.hpp>
#include<SFML/System.hpp>

class missile : public sf::Sprite, sf::Texture
{
 // member functions - perform operations on data members
 public:
  void move(float offsetX, float offsetY);
 private:
 sf::Texture rocket_picture.loadFromFile("/home/neil/game/data/missile.png");
 sf::Sprite missile.setTexture(rocket_picture); 
 missile.setScale(0.01f,0.02f); 
};

#endif


But still does not work I now get this error:

missile.h:13:14: error: expected ‘;’ at end of member declaration
missile.h:13:28: error: expected unqualified-id before ‘.’ token
missile.h:15:13: error: expected ‘;’ at end of member declaration
missile.h:15:20: error: expected unqualified-id before ‘.’ token
missile.h:16:2: error: ‘missile’ does not name a type



You can't call functions outside of functions like you do on line 13-15. If you want these functions to be called each time a missile object is created you should call them from the constructor instead.
Last edited on
Also, inheriting from sf::Sprite isn't a great idea. Inheriting from sf::Texture is an absolutely horrible one.
Like cire said. You want to use composition instead. You can inherit from sf::Transformable if you want to use the move method.
Topic archived. No new replies allowed.