Program wide variables

This is a discussion on Program wide variables within the C++ Programming forums, part of the General Programming Boards category; I am wanting to have a variable that is accessible from any module, so that the 'main' function in moduleA(modA.cpp) ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    26

    Program wide variables

    I am wanting to have a variable that is accessible from any module, so that the 'main' function in moduleA(modA.cpp) can set that value of variable 'bob', while function 'check_status' in moduleB(modB.cpp) can check to see if the value of 'bob' is five. This is just a very simplified version of what I am really trying to do but it should give the basic idea of what I am attempting. I know that one can use a header to declare function that is not in the same module, but when I try doing this (after including the file in both modules) the program won't compile, giving me an error message that says that the variable has been declared multiple times. I know how to do this in VB and QuickBasic, but I can not seem to get it to work in C++. I have not tried using pointers, but I think this might be a way to accomplish what I am attempting, because I would prefer to just create one variable and use it through the whole program, regardless of which module the function is located.

    - JLBShecky
    System
    OS - Microsoft Windows XP Pro
    CPU - AMD Athlon XP 2600+
    Mother Board - Abit KV7
    RAM - 512 Mb DDR (333)

    C++
    Microsoft Visual Studio Pro. 6.0
    MSDN July 2001

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    //Header file
    #ifndef HEADERFILENAME_H_
    #define HEADERFILENAME_H_
    bool isFive( void );
    
    int theNum;
    #endif
    
    //First CPP file
    #include "header.h"
    bool isFive( void ) {
        return ( theNum == 5 );
    }
    
    //Second CPP file
    #include <iostream>
    #include "header.h"
    int main( void ) {
        theNum = 5;
        if( isFive( ) ) std::cout<<"The number is five!"<<std::endl;
        else std::cout<<"The number isn't five!"<<std::endl;
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    26
    Thanks for the input XSquared, I tried the code (which was basicly the same as mine except for a few differances in the names) and got the same error, so I tried the 'fource output' complier option for the first time and it worked. I guess that that was all that I needed to do. Thanks.

    -JLBshecky
    System
    OS - Microsoft Windows XP Pro
    CPU - AMD Athlon XP 2600+
    Mother Board - Abit KV7
    RAM - 512 Mb DDR (333)

    C++
    Microsoft Visual Studio Pro. 6.0
    MSDN July 2001

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    26
    Now lets see if I have the correct. I have one module with a variable in it (that is not declared in any function). If I want to access that variable from functions in other modules all I have to do is include the word 'extern' as the first word of the variable declaration in the other modules and I will no longer get compiler error '2005' and the program will compile properly.

    -JLBShecky
    System
    OS - Microsoft Windows XP Pro
    CPU - AMD Athlon XP 2600+
    Mother Board - Abit KV7
    RAM - 512 Mb DDR (333)

    C++
    Microsoft Visual Studio Pro. 6.0
    MSDN July 2001

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Maybe next time I'll test my code before I post it. Oh well.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    ACtually, you just have to include extern before the variable in the header. Then, just define the variable in one of the modules its included in.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    26
    It seem that the keyword extern does not work with structures. Is there a way to make it so that a structure is visible from more than one module?

    -JLBShecky
    System
    OS - Microsoft Windows XP Pro
    CPU - AMD Athlon XP 2600+
    Mother Board - Abit KV7
    RAM - 512 Mb DDR (333)

    C++
    Microsoft Visual Studio Pro. 6.0
    MSDN July 2001

  8. #8
    nubi
    Guest
    uhm...put the structure in a header file?

  9. #9
    cgoat
    Guest
    Extern is only for actual instances of a struct, not the type itself. What you should do is have a globals.h and globals.cpp file, that would look something like this:

    Code:
    /**
     * Globals.h
     */
    #ifndef _GLOBALS_H_
    #define _GLOBALS_H_
    
    #include "ProgStructs.h" // Has declaration of structure types
    
    // All these variables will be available throughout the program
    extern ProgStruct gTheStruct;
    extern int gTheInt;
    // etc...
    
    #endif
    
    /**
     * Globals.cpp
     */
    #include "Globals.h"
    
    ProgStruct gTheStruct;
    int gTheInt = 0;
    Then you just compile Globals.cpp as part of your program, and have anyone that wants to use those vars include Globals.h

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  2. Editing variables while running a flash program.
    By two31d in forum Tech Board
    Replies: 2
    Last Post: 02-28-2006, 12:27 PM
  3. Parse a program for functions, variables
    By Enu in forum C Programming
    Replies: 2
    Last Post: 02-15-2006, 10:08 AM
  4. Craps Program with Local Variables
    By tigrfire in forum C Programming
    Replies: 12
    Last Post: 11-09-2005, 08:01 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21