Thread: variables across files

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    variables across files

    Hey guys,

    If I declare a global variable in, say, "main.cpp", then try to access it in, say, "file2.cpp", it won't work (thats right, right?). Earlier on, I read in a book that you can declare all of your global variables in a header, then include it in another file, then declare it using "extern *variablename*" in the second file... then, it said something like you can include the header in yet another file and use extern again, and then both files will have access to the globals. I tried it, but then it gave me an error:

    Missing ';' before identifier 'Ship' //this was in globals.h

    That usually means that 'Ship''s header hasn't been included yet... but it WAS. So then I tried not including the header with the globals (as in, I included it in 'main.cpp' but not in the second file), and then deleted all the stuff in the second file that depended on these global variables, and then it worked. Does anybody know why? (Or, for that matter, how I can share the global variables between files?)
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    This goes in a header file, which is included in all the source files which access foo
    extern int foo;

    This goes in ONE source file to declare the variable
    int foo;

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    so...
    Code:
    //global.h
    
    int foo;
    Code:
    //main.cpp
    
    #include "global.h"
    
    extern int foo;
    Code:
    //file2.cpp
    
    #include "global.h"
    
    extern int foo;
    right?... I tried that, but then it gave me an error in global.h...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    403
    show more of your global.h file.. what you showed is perfectly fine.. what is the current error + show the lines around the line it reports the error on.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    All right... this is globals.h
    Code:
    #ifndef _globals_h_
    #define _globals_h_
    
    bool done;
    GFX gfx;
    Ship ship; // <--- error here: "Missing ';' before identifier 'Ship'
    std::vector<Alien> aliens;
    
    #endif
    and this is the first part of main.cpp
    Code:
    #include <windows.h>
    #include "resource.h"
    
    #include "GFX.h"
    #include "Ship.h"
    #include "Bullet.h"
    #include "Alien.h"
    
    #include "main.h"
    #include "StaticDefs.h"
    #include "globals.h"
    
    extern bool done;
    extern GFX gfx;
    extern Ship ship;
    extern std::vector<Alien> aliens;
    and this is part of Rocket.cpp
    Code:
    #include "Rocket.h"
    #include <math.h>
    #include "CoordGeom.h"
    #include "Alien.h"
    #include <vector>
    #include "globals.h"
    
    extern std::vector<Alien> aliens;
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    This is probably because you did not link the class ship correctly

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    *I tried including Ship.h before globals.h in Rocket.cpp, but after it got past that one error, it gave me 4 more linker errors:

    Code:
    "bool  done" (?done@@3_NA) already defined in main.obj
    "class Ship  ship" (?ship@@3VShip@@A) already defined in main.obj
    //etc.
    Last edited by Hunter2; 07-20-2002 at 07:43 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > right?... I tried that, but then it gave me an error in global.h...

    extern goes in front of the declarations in the .h files, not the .cpp files

    #ifndef _globals_h_
    #define _globals_h_

    // add extern here
    extern bool done;
    extern GFX gfx;
    extern Ship ship;
    extern std::vector<Alien> aliens;

    #endif

    and this is the first part of main.cpp

    #include <windows.h>
    #include "resource.h"

    #include "GFX.h"
    #include "Ship.h"
    #include "Bullet.h"
    #include "Alien.h"

    #include "main.h"
    #include "StaticDefs.h"
    #include "globals.h"

    // remove extern from these
    bool done;
    GFX gfx;
    Ship ship;
    std::vector<Alien> aliens;

    and this is part of Rocket.cpp

    #include "Rocket.h"
    #include <math.h>
    #include "CoordGeom.h"
    #include "Alien.h"
    #include <vector>
    #include "globals.h"

    // no need for this, its in globals.h
    // extern std::vector<Alien> aliens;

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ok, thanks! I'll try that later and if I have any problems, I'll post another message
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You could also do it this slick way...
    Code:
    #ifndef GLOBALS_H
    #define GLOBALS_H
    
    #ifdef MAIN_CPP
    #define GLOBAL 
    #else
    #define GLOBAL extern
    #endif
    
    GLOBAL UINT  g_Money;
    GLOBAL BOOL g_Loop;
    
    #endif /* GLOBALS_H */
    That way you don't have to type it twice. Maybe I'm just lazy. Right before you include this header in your main just add #define MAIN_CPP
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Uh... what? Sorry, that #define stuff sorta confuses me... I'll have it sorted out in a couple of days
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Thanks Salem, it worked great! And thanks too, mr.wizard, although I couldn't figure out how yours was supposed to work
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by Hunter2
    Thanks Salem, it worked great! And thanks too, mr.wizard, although I couldn't figure out how yours was supposed to work
    Awww Well if you're curious about that implementation in your specific case, send me a message and I will help you. If you don't care, that's cool too

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Well... since Salem's worked fine, I'd have to say the latter :P
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using mmap for copying large files
    By rohan_ak1 in forum C Programming
    Replies: 6
    Last Post: 05-13-2008, 08:12 AM
  2. accessing all files in a folder.
    By pastitprogram in forum C++ Programming
    Replies: 15
    Last Post: 04-30-2008, 10:56 AM
  3. Help with loading files into rich text box
    By blueparukia in forum C# Programming
    Replies: 3
    Last Post: 10-19-2007, 12:59 AM
  4. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  5. fopen vs. _open (for BIG image files)
    By reversaflex in forum C Programming
    Replies: 3
    Last Post: 04-01-2007, 12:52 AM