Problam with a global class variable

This is a discussion on Problam with a global class variable within the C++ Programming forums, part of the General Programming Boards category; Okay, in a program that I am creating I have run into a problem. I have this one class ( ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    110

    Problam with a global class variable

    Okay, in a program that I am creating I have run into a problem.

    I have this one class ( of which there only needs to be on instance of ) that will be accessed by other classes.

    I have tried placing a pointer in each of the classes then making them all equal a variable placed in the int main() and passed to the constructors.

    The problem with the above is that it continualy segmentation faults when I try to do some work with the main class.

    Now back to the problem I am asking for help with is that if I only use the global variable once it seems to work fine, though if I use it in another class then the linker compaines and states that there are multiple definitions in each of the .o files that are being linked.

    The compiler and linker that I am using are the GNU C/C++ compiler provided with Slackware 8.1 ( I forget which version that it is ).

    Any help will be greatly apreciated.

    Thanks,
    WebmasterMattD
    WebmasterMattD.NET

  2. #2
    stovellp
    Guest

    Talking

    Post some of your code, makes it easier for me. I might be able to understand it then.

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Look up singletons. That's what you want.

    The reason you are getting the error with your global is because you aren't extern'ing it.

    When you include the same global in multiple files, you have to use the keyword extern prior to its declaration in the header.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    tried placing extern before the variable decleration though this time the linker complaines that there is an undefined reference to the externed variable.

    Anything else that should be done or is the linker stuffing up.

    Thanks for your help,
    WebmasterMattD
    WebmasterMattD.NET

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    You have to make sure you also write the declaration (without extern) in exactly one of the source files that includes the externed global.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    Done that, though now it goes back to the original error of claiming that there are multiple declerations of the global variable. ( It is the linker, not the compiler that is complaining ).

    Thanks heaps for your help,
    WebmasterMattD
    WebmasterMattD.NET

  7. #7
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Recap:

    Declare the global in a header with extern prior to it (don't initialize it here).

    #include the header in all source files that use it.

    In 1 and ONLY 1 source file that includes it, write the declaration of the variable again but this time WITHOUT the keyword extern, along with the initialization if it needs it.

    If you still have problems, post the source.

  8. #8
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Here's an elegant alternative to using extern. Worth having a look, even if you don't intend to use it.

    Here's your class:

    [code]
    class MyClass
    {
    private:
    ...
    public:
    ...
    static MyClass& GetStaticInstance();
    };
    [\code]

    Where GetStaticInstance returns a static instance of the object, like so:

    Code:
    MyClass& MyClass::GetStaticInstance()
    {
      static MyClass sc;
      return sc;
    }
    No you can use your global instance like so:

    Code:
    MyClass::GetStaticInstance().DoSomething();

  9. #9
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    ^ That's one form of a singleton if you haven't looked it up yet.

    Use a form of them instead of globals.

  10. #10
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    PolyMorph - Don't you ever sleep?
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  11. #11
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Not when there's programming to be done!

    (so, no, I don't sleep much )

    I have problems. When I'm not programming, or making posts about programming, or at least thinking about programming, I feel guilty about it. Sleep seems like such a waste of time, so I squeeze as much out of a day as I can. I also find that if I go to sleep when I'm not extremely tired, I end up staying awake for a few hours thinking about what to do the next day.

    This is also why I've never been in a long relationship

    I figure that if I become a successful video game programmer, I'll have time to relax later on in life . For now, there's too much to do!

    EDIT: more typos than usual. maybe I should go to sleep, heh.
    Last edited by Polymorphic OOP; 01-21-2003 at 04:11 AM.

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    Thanks for your help Polymorphic OOP,
    I have not done much with global variables in c++, and my main culpurate was the order of operations.

    I guess that it comes down to the old saying that computers do what we tell them to do, though as humans we don't know what it is we are telling them.

    Later,
    WebmasterMattD
    WebmasterMattD.NET

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global variable and prototype
    By Lincoln_Poh in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2008, 04:25 AM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 02:03 AM
  3. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

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