I wanna have a root class (Sprite) which I want shall hold a name and an image (SDL_Surface). But when i am creating an object of Sprite, say spaceship, the program underlines either the first string class or the sdl_surface in the constructor in main.cpp
The compiler will interpret spaceship.gif as if you were passing a structure or class spaceship with a member named "gif" like:
1 2 3 4 5
class CSpaceship
{
// ...
Sometype gif;
};
Seeing from the constructor you posted, it seems you need to pass an instance of the surface instead, something like:
1 2
// assuming that the SDL_Surface constructor takes as string as parameter
Sprite s1("spaceship", "spaceship.gif", new SDL_Surface ( "spaceship.gif" ));
Take a look at this very silly example:
1 2 3 4 5 6 7 8 9 10 11 12 13
class t{
public:
t(string& t){};
};
class z{
public:
z(t* ptr_t){};
};
// then later one you instantiate z
string a("");
z zInstance(new t(a));
Well okey, but how do I make the constructor so it takes a name, and the filename of the image source example: spaceship, "C/Images/spaceship.gif"
The best thing i wanted to do from the start was an Image class which holds a SDL_Surface pointer which will have a automatic reference countering and cleaning of the images.
Just like you did just pass the path to the constructor:
Sprite s1("spaceship", "spaceship.gif", new SDL_Surface ( "c:\\Images\\spaceship.gif" ));
I am not sure but I think that this SDL_Surface class simply creates the source for you:
1 2 3 4 5 6
// here the image is loaded and placed somewhere in the class SDL_Surface
SDL_Surface* YourSurface = new SDL_Surface( "c:\\Images\\spaceship.gif" ));
Sprite s1("spaceship", "spaceship.gif", YourSurface )
// then inside of the class implementation of Sprite you may access
spriteImage->Your_Methods_Here();
well if i put new SDL:Surface ("filename"); it says:
Error: no suitable constructor exists to convert from "const char[14]" to "SDL_Surface"
If I just put another SDL_Surface which I have decleared before the Sprite s1(...); it works, but then the whole idea with sprites is pretty useless ^^
so please post here the constructor for SDL_Surface or most likely....
I actually gave a brief look at internet, I know zero of SDL by the way, but it seems SDL_Surface is a struct . I am not sure what you are doing after passing it to the constructor of your class. In this case you declare :
SDL_Surface* YourSurface;
and pass it to the constructor of sprite and let this class fill this structure.
Are you making two calls to IMG_LOAD ? One in your main and the other at your constructor ? if so, try just calling it once, please change your Sprite class constructor to:
1 2 3 4 5 6 7
Sprite::Sprite(std::string name, std::string filename, SDL_Surface* spriteImage)
:name(name), name_image(filename)
{
// si=IMG_Load(name_image.c_str());
si = spriteImage;// you may left as you were implementing
// by assigning here you can debug more easily
}
Also at your main put a breakpoint at the place where you get the spriteObject reference and then inspect what that memory location has, i.e, inspect the struct. It might have some useful data rather than memory garbage.
You still have, what you pass to Sprite is a pointer to the SDL_Surface, so that inside it you have a reference to this object. You are not creating anything new expect if you call IMG_Load.