Thread: global variables

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    13

    global / static variables

    hi, this should be an easy one, i just havent dealt much with global variables.

    i have an unmanaged c++ project set up with many different classes. Each class has its own .h and .cpp set up correctly. They're included in a standard library that each cpp references. All of this works.

    There is a main cpp file which also references the standard library. all of its functions are global, and can be accessed from any other class or file. Should its variables, declared outside of functions then also be global, and available from any class? They're not. Other classes can't find these variables. When added to the standard library, they get an error because they are created about 20 different times (once per .cpp file that includes the library) despite the "#pragma once" that i have.

    I need these variables to be available everywhere. Am i just missing some syntax? or would a namespace be my solution. I'd like to avoid the latter if at all possible.

    thanks in advance.
    Last edited by shadovv; 10-23-2005 at 11:04 PM.

  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
    They can be global if you want, but all your classes should gain access to them via appropriate parameters passed in function calls. The classes shouldn't care about the actual scope the variable has in the caller.

    Classes which access random global variables declared elsewhere really aren't classes at all.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    13
    I included them as static members of the base class in my class heirarchy. Therefore every class has access to them.

    Still, i'm getting linker errors saying that it they are "unresolved global symbols". Help?!

    (also as a sidenote, should i be able to access it with Base::staticvariable?, i'm not anywhere, but was curious)
    Last edited by shadovv; 10-23-2005 at 11:07 PM.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    I guess most will agree we cant help with your if we dont see some code
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    13
    Sorry, this is an extremely complex program, and i was trying to avoid actually citing code if possible. I'll write an extremely simplified version to get the concept across. Keep in mind that these are all in their own files (as listed by the //<filename>// comments)

    Code:
    //<stdlib.h>//
    class Base;
    class Derived;
    
    #include "Base.h"
    #include "Derived.h"
    Code:
    //<Base.h>//
    class Base
    {
    public:
       static int sNum;
       Base();
    }
    Code:
    //<Base.cpp>//
    #include "stdlib.h"
    Base::Base()
    {
       //initialization stuff
    }
    Code:
    //<Derived.h>//
    class Derived : public Base
    {
    public:
        Derived();
        void Use();
    }
    Code:
    //<Derived.cpp>//
    #include "stdlib.h"
    Derived::Derived()
    {
       //initialization stuff
    }
    void Derived::Use()
    {
      sNum = 5;
    }
    Code:
    //<Main.cpp>//
    #include "stdlib.h"
    
    Base *b;
    
    int main()
    {
       Derived *d = new Derived;
       b = (Base*)d;
       Base *b2 = new Base;
    
       d->Use(); //sets sNum to 5;
       
       cout<<Base::sNum<<endl;
       cout<<b->sNum<<endl;
       cout<<b2->sNum<<endl;
       //should all output 5?
    
       return 0;
    }

    It has always worked up to this point, but when i tried to use a static variable, i started getting problems. It compiles alright, but throws a linker error, that the static variable is an "unresolved external symbol". It doesnt list a specific line, or place in the code, since its a linker error, so i really have no idea what i'm doing wrong.

    Please keep in mind that this is a simplified version and has to stay in the format shown.

    Thanks in advance.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Did you actually define your static data member somewhere?
    Code:
    //<Base.h>//
    class Base
    {
    public:
       static int sNum;
       Base();
    };
    
    int Base::sNum = some_integer_value;

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    13
    That solved it. I never defined it again like that. Thanks a million, this has been driving me crazy.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I was confused too when I first learned this. With static variables of a class, not only do you have to declare them, you also have to define them. My assumption for why: so they can be assigned an initial value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-06-2008, 09:59 AM
  2. scope of global variables
    By laertius in forum C++ Programming
    Replies: 4
    Last Post: 10-15-2006, 01:59 AM
  3. global variables - okay sometimes...?
    By MadHatter in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2003, 04:23 PM
  4. global variables
    By rdnjr in forum Linux Programming
    Replies: 0
    Last Post: 01-07-2003, 10:28 AM
  5. Global variables? Bad! Yes, but to what extent?
    By Boksha in forum C++ Programming
    Replies: 6
    Last Post: 05-26-2002, 04:37 PM