Beginner C++ question
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.
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
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
I tried the #include "file.h" directive but i get variable already declared errors but i''ll try the extern keyword.
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.
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.
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:
Now, you should be able to use the myfirstvar integer variable in
your main module...
Good Luck with your programming,
Edited by - pentium3id on January 1, 2002 11:50:20 PM
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 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
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
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).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement