Thread: Globaly defining variables

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    3

    Angry Globaly defining variables

    I am making a game and i need to reference to a variable that holds a texture so i define it in my main.cpp file, but for some reason it still errors when it goes to compile the other files that use it saying that it cant find the variables, Then when i made a .h file and put the def in there it had a lnk error saying it was multiply defined. Is there some kind of special global variable i hav eto define???????????


    Thanks ~Doug

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    file 1:

    someType someVar;

    file 2:

    extern someType someVar;

    Vola, that's all there is to it. Don't put it in a header.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    look up the extern storage specifier.

    define the variable in 1 file and then declare it as extern wherever else you use it.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Create a global header file, include only in main file.

    Declare all globals as extern in files as needed.

    Use conditional comp
    #ifndef MY_HEADER
    #def MY_HEADER
    //all the header stuff is included only once in a
    //project but header file can still be #included in all files
    #endif
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User hankspears's Avatar
    Join Date
    Mar 2002
    Posts
    14

    Unhappy multiply defined variables



    but what if you are doing an assignment that encourages you to use headers???

    is the #ifndef - #endif loop not working?

  6. #6
    Unregistered
    Guest
    Why use a global?

    Try this (the type will be int in this example):

    int setValue(int x) {
    static int theVariable = NULL;

    if(x != NULL)
    theVariable = x;

    return theVariable;
    }

    int getValue() {
    return setValue(NULL);
    }

    You can now have access to 'theVariable' anywhere in your program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. defining macros or static variables
    By l2u in forum C++ Programming
    Replies: 15
    Last Post: 08-05-2008, 06:28 AM
  2. Defining Variables
    By Beaner in forum C++ Programming
    Replies: 3
    Last Post: 03-10-2005, 05:50 PM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM