• ### Announcements

#### Archived

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

# Beginner C++ question

## Recommended Posts

Matthew123    157
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 on other sites
barazor    122
hmm...i think i might get your drift

in file1.cpp make something like

then you should be able to call it from another file

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

##### Share on other sites
Matthew123    157
I tried the #include "file.h" directive but i get variable already declared errors but i''ll try the extern keyword.

##### 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 on other sites
Prozak    898

"MyVars.h"

#include "myvars.h"

  #ifndef myvarsh_abdg23 //this prevents "alredy defined" errors#define myvarsh_abdg23 // use any string after ifndefextern 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

[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