Archived

This topic is now archived and is closed to further replies.

"Silly lpDD! Start pointing! What? Don't worry it's not rude..."

This topic is 6367 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have my lpDD declared as follows: static LPDIRECTDRAW7 lpDD=NULL; The intention was to have it as a global variable delcared in directdrawglobal.h. Well it''s declared all right, but it doesn''t exactly seem to be golbal. Within my start up direct draw function, it seems to be created fine, with lpDD!=NULL. However, when I check it immediatly after the function, I find it''s NULL. Not exactly what I wanted. I read up in the MSVC++ help on static, and it says it should be used for variables that you want seen in all files. Great, I thought. Should my lpDD be declared extern, or something instead?! Ah, what am I doing?! Help much appreciated, Akira

Share this post


Link to post
Share on other sites
lose the static and just extern it wherever you want to use it.

Or, better yet, hide it in your CDirectDraw class. You are using proper classes yes?

Share this post


Link to post
Share on other sites
Yes, using extern would be the thing to do. Also, make sure that you aren''t accidentally doing something like
if(lpDD = NULL)
...

when you, of course mean
if(lpDD == NULL)
...

Share this post


Link to post
Share on other sites
Not using classes at the moment. Suppose I should. Just wanted to get things running first!

So I''ll have:

directdrawglobal.h

LPDIRECTDRAW7 lpDD=NULL;

myprog.cpp

extern LPDIRECTDRAW7 lpDD;

Yes?

Share this post


Link to post
Share on other sites
Not quite. If you do that and include the .h file in multiple projects, you will get a lot of errors about a variable being initialized more than once. What you could do is have the extern version in the .h file and then the actual initialization in the .cpp file where you set up DirectDraw.

Share this post


Link to post
Share on other sites
So instead, I''ll have:

directdrawglobal.h - include where lpDD etc. are needed

extern LPDIRECTDRAW7 lpDD=NULL;

directdrawsetupfile.cpp - where direct draw setup function is.

LPDIRECTDRAW7 lpDD;

Yes?

Share this post


Link to post
Share on other sites
You are initializing this variable with = NULL; Just leave it with:

extern LPDIRECTDRAW7 lpDD;


Then in your directdrawsetupfile.cpp - where direct draw setup function is.

LPDIRECTDRAW7 lpDD = NULL;

8)

Share this post


Link to post
Share on other sites
Thanks for your replies guys...

But, I''m still getting linker errors. Maybe if I show the chain of includes i have, it''ll make it clear what I''m doing wrong!

mainprog.h
-
--> game.h
-
-->directdraw.h
- -
- -->directdraw24bit.h
- - -->directdrawglobal.h
- - -->directdrawdef.h
- -
- -->directdraw8bit.h
- - -->directdrawglobal.h
- - -->directdrawdef.h
- -
- -->directdrawdef.h
-
-->directinput.h
.
.
etc.

That''s a little flowchart showing what''s #including what going from top-bottom, left-right.

Now, I need lpDD, and a few others available from directdraw24bit.h/directdraw8bit.h, all the way down to mainprog.h. At the moment, Directdrawglobal.h holds all ''extern LPDIRECTDRAW7 lpDD'' .etc, while directdraw.h has the normal declarations. diretdrawdef.h holds all the definitions needed. Also at the moment, the linker is giving out stink about game.obj, and mainprog.obj, already having vars, like lpDD, alreadydefined in DirectDraw.obj.

How do I resolve this? Is my #include system messed up? I suspect so...

Thanks
Akira

Share this post


Link to post
Share on other sites
You can only initialize a variable in one place. Everywhere else, you need to have the extern statement with no initialization. This is where I usually have problems when I get linker errors when dealing with extern. It might mean that you missed putting one extern keyword somewhere.

The ONLY place that you should have lpDD declared without the keyword extern is in one CPP file.

Share this post


Link to post
Share on other sites
Yipes, I transfered the normal(without extern) declarations from directdraw.h to directdraw.cpp. And now it''s linking fine! I thought just having the normal declarations in one file, but where that could be any file, would be ok. Apparently not. Seems like it hased to be a .cpp file. Mysterious me thinks.

Thanks everyone for their replies, and especially jaxon, whos .cpp declaration trick seemed to work!

Gratefully,
Akira

Share this post


Link to post
Share on other sites