Problem with global variables

This is a discussion on Problem with global variables within the C++ Programming forums, part of the General Programming Boards category; Bare with me :/ I have two class definitions in file MyClass.h: Code: #define MYCLASS_H class MyClass { int i; ...

  1. #1
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120

    Unhappy Problem with global variables

    Bare with me :/

    I have two class definitions in file MyClass.h:

    Code:
    #define MYCLASS_H
    
    class MyClass
    {
        int i;
    
        public:
            MyClass(int n):i(n){}
            int GetI(void){return i;}
    };
    
    class MyClass2
    {
        float x;
    
        public:
            MyClass2(float n):x(n){}
            float GetX(void){return x;}
            void DoStuffToX(void);
    };
    I then have a file named globals.h with a global object of type MyClass in a namespace:

    Code:
    #define GLOBALS_H
    
    #ifndef MYCLASS_H
        #include "myClass.h"
    #endif
    
    namespace my_name
    {
        MyClass myObject(234);
    }
    I then have a file called MyMain.cpp which includes both header files and refers to my global MyClass object:

    Code:
    #include <iostream>
    
    #ifndef GLOBALS_H
        #include "globals.h"
    #endif
    
    #ifndef MYCLASS_H
        #include "myClass.h"
    #endif
    
    
    
    int main()
    {
        cout << my_name::myObject.GetI();
        cin.get();
    
      return 0;
    }

    Now, at this point in the operation everything compiles and works fine; we have an output on the screen of "234". Hoorah. The error occurrs when I try adding another file wanting to include globals.h. This one is called MyClass2.cpp:

    Code:
    #ifndef MYCLASS_H
        #include "myClass.h"
    #endif
    #ifndef GLOBALS_H
        #include "globals.h"
    #endif
    
    void MyClass2::DoStuffToX()
    {
        x *= static_cast<float>(my_name::myObject.GetI());
    }
    I try to compile this (all four files) and I get the following linker errors:

    d:/dom/my documents/developing tools/temp programming/globalobjects/myclass2.o: In function `MyClass2:oStuffToX(void)':
    //d/dom/my documents/developing tools/temp programming/globalobjects/myclass2.cpp:9: multiple definition of `my_name::myObject'
    d:/dom/my documents/developing tools/temp programming/globalobjects/mymain.o(.bss+0x0)://D/DEV-C_~1/Include/G__~1/streambuf.h: first defined here
    This is of course a simplified version of a much more complicated program, where I would really like to be able to have a global variable that can be included by more than one file. Any light on this would be much appreciated.

    dt
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,262
    Dont declare globals in a header file. Instead declare them in the .cpp file. Then if you want to use them in another .cpp file, you can redeclare them as 'extern'.

  3. #3
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    Ahhahhh, thankyou. So would this be right?


    Code:
    #file1
    ...
    namespace myNameSpace
    {
       MyClass myObject(34);
    }
    
    #file2
    ...
    extern myNameSpace::myObject(34);
    Thanks again

    dt
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  4. #4
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    Having problems referring to the global in it's namespace. If I use:

    Code:
    using namespace myNameSpace;
    The compiler quite rightly doesn't recognise the namespace (as it's in the .cpp and not included in any headers). And then I am baffled as to how to refer to my global. Have tried randomly:

    Code:
    extern MyClass myNameSpace::MyObject;
    ...and a few other combinations, to no avail. Anyone know how it's done??!



    Another very related question: I would like the global to be an array. At the moment, I have set it as a pointer; then, when I want to initialize it I do something like:

    Code:
    extern MyClass myNameSpace::*MyObject;  // but obvioously not that!!!
    
    myNameSpace::MyObject = new MyClass[100];
    // myNameSpace::MyObject[0] = blah etc....
    Once this is done, will the global be initialzed globally, so that whenever I rfer to it in another file (using extern) it will still contain the values I gave it in intitializing it. (I assume so, but this extern thing is confusing me!)
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    I did something similar in a project long ago... try something like this in your header file:
    Code:
    namespace myNameSpace
    {
        extern MyClass MyObject;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,262
    Quote Originally Posted by hk_mp5kpdw
    I did something similar in a project long ago... try something like this in your header file:
    Code:
    namespace myNameSpace
    {
        extern MyClass MyObject;
    }
    That will work, but you still need to declare the variable in one of the .cpp files as well (declare it normally, not as 'extern').


    To make a global array, you can do:

    [CODE]
    MyObject* mynamespace::MyObjectname;
    // Dont forget to declare this as extern in your header file

    MyObjectname = new MyObject[100];
    [CODE]

    Once this is done, will the global be initialzed globally, so that whenever I rfer to it in another file (using extern) it will still contain the values I gave it in intitializing it. (I assume so, but this extern thing is confusing me!)
    Yes, the variable will be initialized globally.
    The extern keyword just tells the compiler that this variable exists elsewhere globally. Therefore it tells the compiler not to include the variable in the object file, but it still lets the object file use the variable.

  7. #7
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    Thanks guys
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 09-05-2004, 07:42 PM
  2. Replies: 6
    Last Post: 01-02-2004, 01:01 PM
  3. Global variables.. how bad?
    By punkrockguy318 in forum C++ Programming
    Replies: 19
    Last Post: 11-30-2003, 10:53 PM
  4. global variables in qbasic
    By Geo-Fry in forum Game Programming
    Replies: 10
    Last Post: 10-09-2003, 08:53 AM
  5. Replies: 4
    Last Post: 10-17-2002, 11:09 PM

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