Using Globals Variables between files

Started by
3 comments, last by 1kevgriff 23 years, 7 months ago
I have a system of flags for my game, which I decided to put aside into an array: Example1.cpp int flag[250] = 0; //Flag Array all set to zero Well, I want to use the global var in other files so I added to the headers for the files the line: Example2.h extern int flag[250]; Then I try to access it: Example2.cpp int accessflag(); { flag[5] = 1; return 0; } The problem is that I get the error that flag[5] had not been declared. Could some one give me so hints here or give me another (or better) way to do what I'm trying to do. Thanks. Edited by - Coaster Kev on 8/28/00 7:17:14 PM
Advertisement
When you use ''extern'' to include a global that''s been defined in another source file, don''t include the initialization part of it. So you just want this:

extern int flag[250];

-Ironblayde
Aeon Software
"Your superior intellect is no match for our puny weapons!"
I didn''t mean to write the extern that way. I edited the message. It should be like how I originally wrote it.

Still doesn''t work.
Umm... put the ''extern'' statement in the source file you want to use the array in, not the header file, and see if that does it. I''ve never tried putting ''extern'' statements in a header file before so I don''t know if it works.

-Ironblayde
Aeon Software
"Your superior intellect is no match for our puny weapons!"
I fixed it. It seems the problem was that I accidentally declared the global in my header file, and that was the problem. That''s changed and it works like it''s supposed too.

This topic is closed to new replies.

Advertisement