I can't figure out why I'm getting this linker error

Started by
3 comments, last by dave 18 years, 8 months ago

class InstructionList
{
    static std::vector<Instruction*> instructions;
        
    public:
        static void Add(Instruction* instr_) 
        {
            instr_->Initialize();
             nstructions.push_back(instr_);
        }
};
The error is: main.o(.text$_ZN7stratus15InstructionList3AddEPNS_11InstructionE[stratus::InstructionList::Add(stratus::Instruction*)]+0x29): In function `ZNSt6vectorIPN7stratus11InstructionESaIS2_EE3endEv': C:/Dev-Cpp/Bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stl_iterator.h: undefined reference to `stratus::InstructionList::instructions' collect2: ld returned 1 exit status I'm using MinGW. Thanks for any help.
Advertisement
It's been a while but i think you have to give the static member some presence.

class InstructionList

{

static std::vector<Instruction*> instructions;



public:

static void Add(Instruction* instr_)

{

instr_->Initialize();

nstructions.push_back(instr_);

}

};

static std::vector<Instruction*> InstructionList::instructions;

Something like that.

ace
Thanks a lot!

That was it, except you have to remove the second 'static'.
I was practically pounding my head on the keyboard over that one!
ace_lovegrove is right, when using static variables inside a class you need to provide a definition of the variable outside of the class in order to use them. Failure to do so will give you an unresolved external error (VS) or undefined reference (GCC/MinGW).

EDIT: too slow.
Oh yeah, no second static, like i said its been a while.

ace

This topic is closed to new replies.

Advertisement