Archived

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

DirectX and LaMothe code

This topic is 6358 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

Ive been using LaMothe''s "Tricks" book to learn how to use DirectX. I''ve been running into a number of problems but I can''t figure this one out. Up to a point in the book, directx related things were declared in the source file like LPDIRECTDRAW lpdd = NULL; // dd object LPDIRECTDRAW4 lpdd4 = NULL; // dd4 object LPDIRECTDRAWSURFACE4 lpddsprimary = NULL; // dd primary LPDIRECTDRAWSURFACE4 lpddsback = NULL; // dd back surface LPDIRECTDRAWPALETTE lpddpal = NULL; // a pointer to the LPDIRECTDRAWCLIPPER lpddclipper = NULL; // dd clipper etc., along with a few macros. Everything is dandy when these things are declared in my source file. Then he puts all of these things in a header file along with its source file and these things are declared like this in the header extern LPDIRECTDRAW4 lpdd; // dd object extern LPDIRECTDRAWSURFACE4 lpddsprimary; // dd primary extern LPDIRECTDRAWSURFACE4 lpddsback; // dd back extern LPDIRECTDRAWPALETTE lpddpal; // a pointer extern LPDIRECTDRAWCLIPPER lpddclipper; // dd clipper extern LPDIRECTDRAWCLIPPER lpddclipperwin; // dd clipper etc., I include this header file and take the original declarations out and Im greeted with a bunch of errors, such as ..source.cpp(200) : error C2664: ''DirectDrawCreate'' : cannot convert parameter 2 from ''struct IDirectDraw4 ** '' to ''struct IDirectDraw ** '' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast ...source.cpp(208) : error C2065: ''lpdd4'' : undeclared identifier ...source.cpp(217) : error C2227: left of ''->SetCooperativeLevel'' must point to class/struct/union .. many more similar errors Error executing cl.exe. when I comment out the external dec''s in the header file and put the other declarations in my source file back, there are no problems but I cant figure out why I can''t use the header file the way it is and save space in my source file. I know this isnt much to go on but I really need help and would appreciate any advice. Thanks

Share this post


Link to post
Share on other sites
You can''t declare a variable in a header. Perhaps you don''t know what the keyword extern means so I will try to explain it for you.

Let''s say that you have a global variable that you wan''t to use in more files than the one it was declared in. Then you declare the variable like normal.
When you wan''t to be able to use the variable in another source file you put the keyword extern in front of it.
Code example:
File1.cpp
LPDIRECTDRAW lpdd = NULL;

File2.cpp
extern LPDIRECTDRAW lpdd;

After this the linker handles it all for you
And that''s all there is to it really.

The reason you get errors when removing the declarations but keeping the externals is that there are no variables then, just a reference to a variable that doesn''t exist.

Share this post


Link to post
Share on other sites
sure you can declare a variable in a header...

file.h-
extern int x = 0;

is a declaration and definition then you can use x in any file in the program.

if it was only

file.h-
extern int x;

and there was no definition or whatever you would get an unresolved external error.

Share this post


Link to post
Share on other sites