Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

class design problem

19 views
Skip to first unread message

langley

unread,
May 23, 2013, 1:43:30 PM5/23/13
to
I have been trying to write a very simple class. It represents a
'missile' in a game. It will not compile and i need help with it please.
It is c++ using sfml 2.0 and my compiler is using c++11:

#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/game/data/missile.png");
missile.setTexture(rocket_picture);
missile.setScale(0.01f,0.02f);
};
------------------
I get the following error
> 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 dont know what this is about as there is clearly a ; at the end of the
line:
static sf::Texture
rocket_picture.loadFromFile("/home/game/data/missile.png");

Can you see any problems with this class?

I have tried re-writing it to:
#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/game/data/missile.png");
sf::Sprite missile.setTexture(rocket_picture);
missile.setScale(0.01f,0.02f);
};

#endif

I then get these 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
------
missile is clearly type sf::Sprite.
why am i getting these mistakes?

Please help me

Francis Glassborow

unread,
May 23, 2013, 3:34:26 PM5/23/13
to
On 23/05/2013 18:43, langley wrote:
> I have been trying to write a very simple class. It represents a
> 'missile' in a game. It will not compile and i need help with it please.
> It is c++ using sfml 2.0 and my compiler is using c++11:
>
> #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
What data members? you do not declare any in the following lines.
> static sf::Texture
assuming that sf::Texture is a type defined in one of the included files
it needs to be followed by a name (id) of either a function or a data
member.
> rocket_picture.loadFromFile("/home/game/data/missile.png");
but that line is neither. It looks like an attempt to call a function
loadFromFilethat is a member of rocket_picture passing it a file name.
C++ does not allow a function call here.

> missile.setTexture(rocket_picture);
missile is the class you are currently defining do what is it doing here?
> missile.setScale(0.01f,0.02f);
> };


I think you need to go back and have another study of how to define a
class in C++.

Francis

nvangogh

unread,
May 23, 2013, 5:59:05 PM5/23/13
to
OK - thanks for your reply. You & others in this group have helped me
many times before (i accidently posted the original message with another
profile). I am taking another look at this. But perhaps I did make some
mistakes because it is the first time i am trying to do something using
a library outside the standard library. What I have done is a lot of
theory study of C++ and now when i try to apply (what i thought i
learned) it does not seem to work. I thought to help myself learn a
little more i would try something practical and relatively
straightforward. And you know it is getting me thinking more. It is a
very good learning experience - but very frustrating.

Basically I have written a test program that uses sfml (C++ library) to
draw a sprite on the screen. The sprite is an image of a missile that
moves from the bottom of the screen to the top. I am trying to refine
this test program (that works) by now implementing the sprite as a
'class missile'.

If I cannot success in getting the class to work tonight i will show the
original test program tomorrow and ask if you or anybody else can show
what the class should look like. It should only be a few lines as there
is only one public function (move() ) and some declarations to set an
image / picture for a sprite. It should be very easy like i wrote before.

thank you.


nvangogh

unread,
May 24, 2013, 3:29:55 PM5/24/13
to

I have tried for many hours and failed to get this working. I have
redefined the class and it does compile - but it does not do exactly as
the 'non class' code below does.

Below is the test program that simply displays a sprite and moves it up
the y axis following a press of the left shift key. Now the sfml stuff
is not really important to my problem but i include it for completeness.

1 - original program - does not use class

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

int main()
{
// sfml stuff - not relevant here
sf::RenderWindow mywindow(sf::VideoMode(800,600), "missile testing",
sf::Style::Fullscreen);
mywindow.setVerticalSyncEnabled(true);

// THIS IS THE SECTION I WANT TO PUT INTO A CLASS MISSILE
sf::Texture rocket_picture;
rocket_picture.loadFromFile("/home/game/images/missile.jpg");
sf::Sprite missile;
missile.setTexture(rocket_picture);
missile.setScale(0.2f,0.2f);
missile.setPosition(350,400);
//END OF SECTION

// sfml stuff not relevant here
sf::Vector2f position(400,500);
sf::Vector2f pos(400,500);
sf::Time elapsed;
sf::Clock clock;
bool firemissile= false;

bool running = true;
while(mywindow.isOpen())
{
elapsed = clock.restart();
pos = missile.getPosition();
sf::Event event;
while (mywindow.pollEvent(event))
{
if((event.type == sf::Event::KeyPressed) && (event.key.code ==
sf::Keyboard::Escape))
mywindow.close();

if((event.type == sf::Event::KeyPressed) && (event.key.code ==
sf::Keyboard::LShift))
{
// fire 1 missile!
firemissile = true;
}
}
if(firemissile)
{

if(pos.y >= 10)
// NEEDS TO BE A MEMBER FUNCTION
missile.move(0, -200 * elapsed.asSeconds() );

}

mywindow.clear();
mywindow.draw(missile);
mywindow.display();
} // while window is open
return EXIT_SUCCESS;
}//main
-------------------------------
Ok - so the above code works. But what i am seeking to do is to create a
class called 'missile'. This class - will only have one function:
missile.move(0, -200 * elapsed.asSeconds() );

Now the class missile simply represents a sprite. The code that creates
and initialises a sprite in sfml are these few lines:

sf::Texture rocket_picture;
rocket_picture.loadFromFile("/home/game/images/missile.jpg");
sf::Sprite missile;
missile.setTexture(rocket_picture);
missile.setScale(0.2f,0.2f);
missile.setPosition(350,400);

There are two sfml (c++) classes here namely sf::Texture and sf::Sprite.
loadFromFile is a member function of Texture.
setTexture, setScale and setPosition are member functions of sf::Sprite.
Each of these classes has numerous other member functions as well.

So the first step in creating my class is to make sure that it inherits
from these base classes. This is right, isn't it?

I got into a terrible mess on my first attempt and spent a lot of time
trying to correct what i did wrong. This is the re-write I did of the
class missile in a file called missile.h

2. Class missile

#ifndef MISSILE_H
#define MISSILE_H

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

class missile : public sf::Sprite, sf::Texture
{
public:

missile()
{
sf::Texture rocket_picture;
rocket_picture.loadFromFile("/home/game/images/missile.jpg");
rocket.setTexture(rocket_picture);
rocket.setScale(0.2f,0.2f);
rocket.setPosition(350,400);
}

private:
static sf::Texture rocket_picture;
sf::Sprite rocket;

};

#endif

Basically - this class should incorporate all the data that is in the
original program concerning the missile. I just wanted to concentrate on
getting it to work and left out the only public member function -
'move()' until i get it working.

Now I wrote another test program to act as a driver to see if i could
use this class.

3. Driver program for class missile
// test of missile class
#include <iostream>
#include<SFML/Graphics.hpp>
#include<SFML/System.hpp>
#include "missile.h"

int main()
{
// sfml stuff
sf::RenderWindow mywindow(sf::VideoMode(800,600),"missile test",
sf::Style::Fullscreen);
mywindow.setVerticalSyncEnabled(true);

// object created
missile mymissile;

// sfml stuff
sf::Vector2f position(400,500);
sf::Vector2f pos(400,500);
sf::Time elapsed;
sf::Clock clock;
bool firemissile= false;


bool running = true;
while(mywindow.isOpen())
{
elapsed = clock.restart();
pos = mymissile.getPosition();
std::cout << pos.x << "x" << pos.y << std::endl;
sf::Event event;
while (mywindow.pollEvent(event))
{
if((event.type == sf::Event::KeyPressed) && (event.key.code ==
sf::Keyboard::Escape))
mywindow.close();

if((event.type == sf::Event::KeyPressed) && (event.key.code ==
sf::Keyboard::LShift))
{
// fire 1 missile!
firemissile = true;
}
}
if(firemissile)
{
// NOT IMPLEMENTED
// if(pos.y >= 10)

//mymissile.move(0, -200 * elapsed.asSeconds() );

}

mywindow.clear();
mywindow.draw(mymissile);
mywindow.display();
} // while window is open
return EXIT_SUCCESS;
}//main

-------------------
I compile this program with
g++ -std=c++11 missile_class_test.cpp -o cmissile -lsfml-graphics
-lsfml-window -lsfml-system

There are no compile errors and the program runs - but it just displays
a blank screen - it does not show the sprite. Therefore the class does
not do exactly what the code in the original program did.

I had problems understanding how to initialize the data members of the
class because in the books on c++ the examples of constructors do not
show multiple lines of code like this. Is the constructor the problem here?

The output of
std::cout << pos.x << "x" << pos.y << std::endl; is 0x0 when run. This
means that the line in the constructor that is >
rocket.setPosition(350,400);
has no effect.

I do not undersatnd why there are no error messages being generated.
In any case can you see where i have gone wrong in defining this class?

Francis Glassborow

unread,
May 24, 2013, 7:19:53 PM5/24/13
to
On 24/05/2013 20:29, nvangogh wrote:

> 2. Class missile
>
> #ifndef MISSILE_H
> #define MISSILE_H
>
> #include<SFML/Graphics.hpp>
> #include<SFML/System.hpp>
>
> class missile : public sf::Sprite, sf::Texture

I doubt that using inheritance helps here.

Try this (you will have to tweak it a bit to line up the SFML stuff

class missile {
public:
// ctor
missile(std::string rp_source = "/home/game/images/missile.jpg",
float xscale = 0.2, float yscale = 0.2,
int xpos = 350, int ypos = 400){
rocket_picture.loadFromFile(rp_source.c_str());
rocket.setTexture(rocket_picture);
rocket.setScale(xscale, yscale);
rocket.setPosition(xpos, ypos);
}
// now add member functions to manipulate the object
private:
sf::Texture rocket_picture;
sf::Sprite rocket;
};



> {
> public:
>
> missile()
> {
> sf::Texture rocket_picture;
I suspect your error is in the line above because that declare a purely
local rocket_picture that will hide the member with the same name.
> rocket_picture.loadFromFile("/home/game/images/missile.jpg");
> rocket.setTexture(rocket_picture);
> rocket.setScale(0.2f,0.2f);
> rocket.setPosition(350,400);
> }
>
> private:
> static sf::Texture rocket_picture;
you really want rocket_picture to be a static member you will need to
define it and initialise it once which will mean adding a way to trace
that it has been initialised so as to avoid repeat initialisation.
However it makes more sense to make this an ordinary data member because
that will allow you to have different images simultaneously.


> sf::Sprite rocket;
>
> };
>
> #endif

nvangogh

unread,
May 27, 2013, 7:08:29 AM5/27/13
to
Thank you very much indeed - i got it working.!
0 new messages