Thread: static instance variable linker error

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    14

    static instance variable linker error

    I've been having a linker error in one of my programs and I dont understand. It says there's an undefined reference with one of my static instance variables yet it is clearly listed in both the class header and the implementation of the functions. Here is a sample code that resembles the code I'm using.

    The code for foo is seperate because it will be a seperate header implementation file when it is really used.
    Code:
    #include <iostream>
    using namespace std;
    
    class foo
    {
            private:
                    static int death;
    
            public:
                    static void init();
    
                    void print();
    };
    
    void foo::init()
    {
            death = -5;
    
    }
    
    void foo::print()
    {
            cout << death << endl;
    }
    
    int main()
    {
            foo myFoo;
    
            foo::init();
    
            for(int i = 0; i < 10; i++ )
                    myFoo.print();
    
            return 0;
    }
    When I try to compile it I get the following error:

    test.o:test.cpp.text+0x105): undefined reference to `foo::death'
    test.o:test.cpp.text+0x117): undefined reference to `foo::death'

    Also when I do the following objdump: (objdump -t test.o | grep death)

    I find [ 24](sec 0)(fl 0x00)(ty 0)(scl 2) (nx 0) 0x00000000 __ZN3foo5deathE in the symbol table which leads me to believe that the linker is being dumb. Of course it's probably something I did, but from example code on the web of using static instance variables I shouldn't be having this problem. Can anyone help?

    Thanks

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You need to define and initialize the static member variable outside the class definition, you only declared it in the class. Something like this after the class definition:
    Code:
    int foo::death = -5;

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    I have this problem sometimes. I think that it is the linker. You can solve the problem by treating the int within the class declaration as a declaration and therefore specifying another int definition elsewhere.
    Code:
    void foo::print()
    {
            cout << death << endl;
    }
    int foo::death;
    int main()
    {
            foo myFoo;
    
            foo::init();
    
            for(int i = 0; i < 10; i++ )
                    myFoo.print();
    
            return 0;
    }
    Sometimes (for me using Bloodshed) this gives me a multiple definitions error, and sometimes it works. Try it out.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Sometimes (for me using Bloodshed) this gives me a multiple definitions error, and sometimes it works.

    It should be done once inside a cpp file, not in the header file.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    14
    Adding the int foo::death; before the definition worked. Thanks a bunch.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  3. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM