Archived

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

gameprogrammerwiz

passing functions through files

Recommended Posts

ok, here's my code: (i'll explain my problem later) ================================================================= // =================== // = main.h = // =================== #include ddraw.h #include "ddutil.h" ... LPDIRECTDRAWSURFACE lpddsback = NULL; ... // =================== // = main.cpp = // =================== #include "main.h" #include "drawtext.h" ... draw_text(...); ... // =================== // = drawtext.h = // =================== void draw_text(char* str, int x, int y, int color); // =================== // = drawtext.cpp = // =================== #include ddraw.h #include "drawtext.h" void draw_text(char* str, int x, int y, int color) { HDC hdc; if (lpddsback->GetDC(&hdc) == DD_OK) { SetBkMode(hdc, TRANSPARENT); if(color == 0) { SetTextColor(hdc, RGB(0, 0, 0)); } if(color == 1) { SetTextColor(hdc, RGB(255, 255, 255)); } if(color == 2) { SetTextColor(hdc, RGB(255, 0, 0)); } TextOut(hdc, x, y, str, strlen(str)); lpddsback->ReleaseDC(hdc); } } ================================================================= now, i get these errors: C:\Windows\Desktop\Magesticon\drawtext.cpp(13) : error C2065: 'lpddsback' : undeclared identifier C:\Windows\Desktop\Magesticon\drawtext.cpp(13) : error C2227: left of '->GetDC' must point to class/struct/union C:\Windows\Desktop\Magesticon\drawtext.cpp(22) : error C2227: left of '->ReleaseDC' must point to class/struct/union Error executing cl.exe. and if i try to include "main.h" in "drawtext.cpp", it gives me 100+ errors for every line in main.h saying that its a redefinition. i have no idea what wrong here...any ideas? but i think for some reason, i need "main.h" to be included in "drawtext.cpp" so that it can pass the "LPDIRECTDRAWSURFACE lpddsback = NULL;" into "drawtext.cpp" so that it can use it. but if i do try to include "main.h" i get those 100+ errors, like i said. Edited by - gameprogrammerwiz on 6/14/00 1:43:50 AM

Share this post


Link to post
Share on other sites
Somehow you need to define lpddback in drawtext.h
I suggest defining them in drawtext, and including drawtext.h at the top of main.
~V''lion

I came, I saw, I got programmers block.
~V''lion

Share this post


Link to post
Share on other sites
Create two headers file, one called external.h and the other called global.h

In external.h, declare all the global variables as extern, like


extern LPDIRECTDRAW7 lpdd;


In the globals.h, declare all globals like


LPDIRECTDRAW7 lpdd = NULL;


Every cpp that wants access to lpdd include external.h.

Only 1 cpp (typically main.cpp) should include the global.h

That''s the idea how to use global variables. Note though placing all of them in 1 file gets very nasty..

..

Share this post


Link to post
Share on other sites
Generally, actual instances of variables should be in a single .CPP file. And you make extern references to them in .H files. That solves the problem and lets you include your .H files wherever they are needed.

Share this post


Link to post
Share on other sites