SFML community forums
Help => General => Topic started by: ravenheart 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?
-
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: class object: public Drawable {...}
but it doesnt know sf::Drawable there what am i doing wrong?
-
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.
-
well :
object.h:
class object;
object.cpp
#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
#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 : 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?
-
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" !
-
is the default constructor not enough?
or do i need to add something cause of the Image and Sprite?
-
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 ;)
-
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
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
#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 ===|
-
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.
-
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.
-
is the default constructor not enough?
I did not see any object(); in your class.