undefined reference to static member variable

Posted by Max on Stack Overflow See other posts from Stack Overflow or by Max
Published on 2010-05-26T20:58:53Z Indexed on 2010/05/26 21:01 UTC
Read the original article Hit count: 214

Filed under:
|

Hi. I have this class that has a static member. it is also a base class for several other classes in my program. Here's its header file:

#ifndef YARL_OBJECT_HPP
#define YARL_OBJECT_HPP

namespace yarlObject
{
    class YarlObject
    {
    // Member Variables
        private:
            static int nextID; // keeps track of the next ID number to be used
            int ID; // the identifier for a specific object

    // Member Functions
        public:
            YarlObject(): ID(++nextID) {}
            virtual ~YarlObject() {}

            int getID() const {return ID;} 

    };
}

#endif

and here's its implementation file.

#include "YarlObject.hpp"

namespace yarlObject
{
    int YarlObject::nextID = 0;
}

I'm using g++, and it returns three undefined reference to 'yarlObject::YarlObject::nextID linker errors. If I change the ++nextID phrase in the constructor to just nextID, then I only get one error, and if I change it to 1, then it links correctly. I imagine it's something simple, but what's going on?

© Stack Overflow or respective owner

Related posts about c++

Related posts about static-members