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

Author Topic: drawable class  (Read 5890 times)

0 Members and 1 Guest are viewing this topic.

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
drawable class
« on: October 03, 2008, 06:43:05 pm »
hi i wanted to create a class derived from sf::Drawable that can be drawed by sf::RenderWindow
it should be a mix of the MyPicture class and something

so i want my calss to have an image and a sprite and some more things;

hoe could something like thast look?

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
Re: drawable class
« Reply #1 on: October 03, 2008, 08:03:33 pm »
Quote from: "ravenheart"
hi i wanted to create a class derived from sf::Drawable that can be drawed by sf::RenderWindow
it should be a mix of the MyPicture class and something

so i want my calss to have an image and a sprite and some more things;

hoe could something like thast look?


hmm i have a problem i want to create a file object.h

in this:
Code: [Select]
class object: public Drawable {...}
but it doesnt know sf::Drawable there what  am i doing wrong?

Kreeg

  • Full Member
  • ***
  • Posts: 115
    • View Profile
drawable class
« Reply #2 on: October 03, 2008, 08:44:31 pm »
It's not a problem with SFML, It's a C++ problem ! Anyway I'm going to help. So what do you want to do exactly with a sf::Drawable ? A derived class might not be the key to resolving your problem.

I see some reasons why this code won't compile. Is there any 'using namespace sf;' in your code ? If it's the case I can't tell more knowing what you gave us, except that there should be no 'using namespace' in a public interface. Otherwise, try replacing Drawable by sf::Drawable and tell us if it compiled without problems.
Attention (va) aux (sur) messages (mon) subliminaux, (blog) camarade !

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
drawable class
« Reply #3 on: October 04, 2008, 09:11:03 am »
well :

object.h:
Code: [Select]
class object;

object.cpp


Code: [Select]
#include "object.h"
#include <SFML/Graphics.hpp>
#include <iostream>
#include <iomanip>
#include <sstream>

using namespace sf;
using namespace std;


class object
{
    public:

    Image Bild;
    Sprite Abbild;

    float x_pos;
    float y_pos;
    float height;
    float width;

    bool collides(object &partner);
    void LoadFromFile(const std::string& Filename);
};

void object::LoadFromFile(const std::string& Filename)
{
    Bild.LoadFromFile(Filename);
}


main.cpp

Code: [Select]
#include <iostream>
#include "object.h"
#include <SFML/Graphics.hpp>
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;
using namespace sf;


/*class object
{
    public:

    Image Bild;
    Sprite Abbild;

    float x_pos;
    float y_pos;
    float height;
    float width;

    bool collides(object &partner);
    void LoadFromFile(const std::string& Filename);
};

void object::LoadFromFile(const std::string& Filename)
{
    Bild.LoadFromFile(Filename);
}
*/

int main()
{
    object x;
    RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
    x.LoadFromFile("h.jpg");

    return 0;
}


returns :
Code: [Select]
C:\Programme\CodeBlocks\MEINS\Object\main.cpp|35|error: aggregate `object x' has incomplete type and cannot be defined|

when i dont split the data and write it all in main.cpp it worksd fine, wqhy?

Kreeg

  • Full Member
  • ***
  • Posts: 115
    • View Profile
drawable class
« Reply #4 on: October 04, 2008, 09:16:56 am »
You forgot to add a constructor to you object :D
I repeat : you should NOT use 'using namespace' in a public interface. Take them away from "object.h" !
Attention (va) aux (sur) messages (mon) subliminaux, (blog) camarade !

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
drawable class
« Reply #5 on: October 04, 2008, 09:46:41 am »
is the default constructor not enough?

or do i need to add something cause of the Image and Sprite?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
drawable class
« Reply #6 on: October 04, 2008, 10:26:37 am »
If object.h only contains the forward declaration of the object class, you won't be able to do anything with it. You must put the class definition in the header, not in the cpp file.

You should definitely practice more C++ before trying to build SFML applications, I don't think you have all the necessary skills to do it.

We'll still be glad to help you, but we won't teach you C++ on this forum you know ;)
Laurent Gomila - SFML developer

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
drawable class
« Reply #7 on: October 04, 2008, 07:30:10 pm »
when i put it in the .h file it doesnt know sf::Image

and i surely may not write any includes in the header?

( it´s something hard to do first things that are not in any book)

object.h

Code: [Select]
class object
{
    public:

    Image Bild;
    Sprite Abbild;

    float x_pos;
    float y_pos;
    float height;
    float width;

    object();
    bool collides(object &partner);
    void LoadFromFile(const std::string& Filename);
};



object.cpp

Code: [Select]
#include "object.h"
#include <SFML/Graphics.hpp>
#include <iostream>
#include <iomanip>
#include <sstream>

using namespace sf;
using namespace std;


void object::LoadFromFile(const std::string& Filename)
{
    Bild.LoadFromFile(Filename);
}


errors:

    C:\Programme\CodeBlocks\MEINS\Object\object.h|5|error: `Image' does not name a type|
    C:\Programme\CodeBlocks\MEINS\Object\object.h|6|error: `Sprite' does not name a type|
    C:\Programme\CodeBlocks\MEINS\Object\object.h|15|error: expected unqualified-id before '&' token|
    C:\Programme\CodeBlocks\MEINS\Object\object.h|15|error: expected `,' or `...' before '&' token|
    C:\Programme\CodeBlocks\MEINS\Object\object.h|15|error: ISO C++ forbids declaration of `parameter' with no type|
    C:\Programme\CodeBlocks\MEINS\Object\object.cpp|12|error: prototype for `void object::LoadFromFile(const std::string&)' does not match any in class `object'|
    C:\Programme\CodeBlocks\MEINS\Object\object.h|15|error: candidate is: void object::LoadFromFile(int)|
    C:\Programme\CodeBlocks\MEINS\Object\object.cpp||In member function `void object::LoadFromFile(const std::string&)':|
    C:\Programme\CodeBlocks\MEINS\Object\object.cpp|13|error: `Bild' was not declared in this scope|
    C:\Programme\CodeBlocks\MEINS\Object\object.cpp|13|warning: unused variable 'Bild'|
    ||=== Build finished: 8 errors, 1 warnings ===|


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
drawable class
« Reply #8 on: October 04, 2008, 07:54:35 pm »
Of course you need to include <SFML/Graphics.hpp> in your header so that your class has the definition of the SFML classes you use.
Laurent Gomila - SFML developer

MrQuizzles

  • Newbie
  • *
  • Posts: 15
    • View Profile
drawable class
« Reply #9 on: October 05, 2008, 09:42:35 am »
Also, try looking up the term "encapsulation". It's a very important principle of object-oriented design.

Basically:

Make it so that your objects can only be used in exactly the way you want them to be used.
Make the implementation of the object irrelevant to its usage.



This is mainly in response to the fact that you made everything public. It's actually very bad form to do so and can cause difficulties working with the class.

Kreeg

  • Full Member
  • ***
  • Posts: 115
    • View Profile
drawable class
« Reply #10 on: October 06, 2008, 01:50:28 pm »
Quote from: "ravenheart"
is the default constructor not enough?


I did not see any object(); in your class.
Attention (va) aux (sur) messages (mon) subliminaux, (blog) camarade !

 

anything