void CSDLmain::LoadIMG(char *image_path)
{
image = IMG_Load ( image_path );
//debug stuff, commented-out
}
void CSDLmain::BlitIMG(SDL_Surface *screenSurface, int x_pos, int y_pos)
{
SDL_Rect rcDest = { x_pos, y_pos, 0, 0 };
SDL_BlitSurface ( image, NULL, screenSurface, &rcDest );
SDL_FreeSurface ( image );
}
[C++/SDL] private static vars?
hey,
i made a class with a private static variable SDL_Surface *image;
and it is giving me troubles.
error:
[Linker error] undefined reference to `CSDLmain::image'
P.S.: it's the same if i put in public, but.. it does work if i put it as a global var, but i do not want that.
your static variable has to be both declared and initialized.
Im guessing you have not initialzed it.
Typically you have
class A
...
private:
static Surface srf; // declare the static variable
...
in the header file, and
Surface A::srf = NULL; // the actual definition goes here (if you forget this you are likely to get a linker error like yours)
in the corresponding source file
[Edited by - pulpfist on May 17, 2006 2:52:29 PM]
Im guessing you have not initialzed it.
Typically you have
class A
...
private:
static Surface srf; // declare the static variable
...
in the header file, and
Surface A::srf = NULL; // the actual definition goes here (if you forget this you are likely to get a linker error like yours)
in the corresponding source file
[Edited by - pulpfist on May 17, 2006 2:52:29 PM]
so, this should be it then?
main.cpp In function `int SDL_main(int, char**)':
35 main.cpp conflicting declaration 'SDL_Surface CSDLmain::image'
23 SDLmain.h 'CSDLmain::image' has a previous declaration as `SDL_Surface*CSDLmain::image'
35 main.cpp conversion from `int' to non-scalar type `SDL_Surface' requested
if i put a asterix after SDL_Surface it says it's redeclared and if i remove the SDL_Surface it says it's private and.. if i put the *image to public it says "undefined reference" again...
ty.
SDL_Surface CSDLmain::image = NULL;
main.cpp In function `int SDL_main(int, char**)':
35 main.cpp conflicting declaration 'SDL_Surface CSDLmain::image'
23 SDLmain.h 'CSDLmain::image' has a previous declaration as `SDL_Surface*CSDLmain::image'
35 main.cpp conversion from `int' to non-scalar type `SDL_Surface' requested
if i put a asterix after SDL_Surface it says it's redeclared and if i remove the SDL_Surface it says it's private and.. if i put the *image to public it says "undefined reference" again...
ty.
You got something like this right?
That should work.
The = NULL thing is not needed btw since initialization to zero is done atomatically for static member variables.
class CSDLmain{private: static SDL_Surface* image;};(If you dont have a CSTLmain.cpp file, make sure this next line is outside and after the class definition, and give your .h file inclusion guards, so that if you happen to include it several times you dont get the redeclared error)SDL_Surface* CSDLmain::image;
That should work.
The = NULL thing is not needed btw since initialization to zero is done atomatically for static member variables.
Quote:Original post by pulpfist
your static variable has to be both declared and initialized.
Im guessing you have not initialzed it.
Declared and defined.
A variable can be defined but uninitialized.
int i; is an example.
i do have a CSDLmain.cpp file, tried it, didn't work :/
i had the inclusion guards from the get go.
and nope, still nothing, or should i say, the same.
i had the inclusion guards from the get go.
and nope, still nothing, or should i say, the same.
Hmm Im starting to wonder if you have included the CSDLmain.cpp file in your project correctly.
Are you getting similar errors for the GetIMG and BlitIMG functions too?
You got the line
#include "CSDLmain.h"
at the top of CSDLmain.cpp file?
If you have the exact same errors as you described earlier Im supprised indeed.
Sorry if I have confused you.
If you post the complete source code its easier to see if is a code problem or not...
Are you getting similar errors for the GetIMG and BlitIMG functions too?
You got the line
#include "CSDLmain.h"
at the top of CSDLmain.cpp file?
If you have the exact same errors as you described earlier Im supprised indeed.
Sorry if I have confused you.
If you post the complete source code its easier to see if is a code problem or not...
The problem here is that you are using a static class member variable where I don't think you want to. Do a little research on static class member variables to see how they work.
(Just a general idea but could/should be worded better)
Static members are kind of like global variables inside the classes namespace in the way they do not belong to any single object but to the class itself.
For example:
Static member variables have their use but I don't think it is what you want in this case as each time you load an image you will a)lose the original image and possibly b)leak memory.
Try taking the static part away and this should work fine.
[edit] to get your code above working with a static member
(Just a general idea but could/should be worded better)
Static members are kind of like global variables inside the classes namespace in the way they do not belong to any single object but to the class itself.
For example:
class funky{public: static int static_member;public: int normal_member;//...};funky foo;foo.normal_member = 111; // a normal member variable // because this is a static member variablefoo.static_member = 666; // this should give your compile time errorfunky::static_member = 777; // this should be fine - notice no object is used
Static member variables have their use but I don't think it is what you want in this case as each time you load an image you will a)lose the original image and possibly b)leak memory.
Try taking the static part away and this should work fine.
[edit] to get your code above working with a static member
// note it isn't in the constructor and looks like a global in C// with slightly different syntaxSDL_Surface *CSDLmain::image = NULL;void CSDLmain::LoadIMG(char *image_path){ CSDLmain::image = IMG_Load ( image_path ); //debug stuff, commented-out}void CSDLmain::BlitIMG(SDL_Surface *screenSurface, int x_pos, int y_pos){ SDL_Rect rcDest = { x_pos, y_pos, 0, 0 }; SDL_BlitSurface ( CSDLmain::image, NULL, screenSurface, &rcDest ); SDL_FreeSurface ( CSDLmain::image );}
yes, I think evillive2 is right. I would try that, and if that dosen't work, then you might want to post more code.
Chad.
Chad.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement