Archived

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

Matthew123

Beginner C++ question

Recommended Posts

I''m new to C++ and have a question. I''m making a simple game engine and want each part to be in different source files. The only problem is I don''t know how to use variables and functions from a seperate source file in another source file. Can anybody help me. Thanks in advance.

Share this post


Link to post
Share on other sites
hmm...i think i might get your drift

in file1.cpp make something like

extern int somenumber;

then you should be able to call it from another file


or maybe you should just use the #include "file.h" directive

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
if you want to make a global variable there are two steps:

put the variable in a header file with extern
put the variable in a source file without extern

then simply include the header where you need to.

Share this post


Link to post
Share on other sites
I had that poblem too:

first create a header file for your variables, something like
"MyVars.h"

in your main module, write
#include "myvars.h"

This is the code for your MyVars header:
  
#ifndef myvarsh_abdg23 //this prevents "alredy defined" errors

#define myvarsh_abdg23 // use any string after ifndef


extern int myfirstvar; // your variable



#endif // this is at end of ile, after all code




Now, you should be able to use the myfirstvar integer variable in
your main module...

Good Luck with your programming,

[Hugo Ferreira][Positronic Dreams][]
"Research is what I'm doing when I don't know what I'm doing."
- Wernher Von Braun (1912-1977)



Edited by - pentium3id on January 1, 2002 11:50:20 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Use extern. It does what it sounds like: "externalizes" the variable. Declare it in one file with extern and you can use it in other files (without the extern).

Share this post


Link to post
Share on other sites