-1

My question could be silly but I am having following problem.

Following piece of code works just fine. I have list that is global or local and can instantiate and push values correctly.

class THistory
{
public:
    UInt32          index;
    UInt32          navToID;
};

//Works Fine
class ML{
    public:
    static THistory *hist2;
};

main.cpp;
ML::hist2 = new THistory[HISTORY_BUFFER_SIZE];//global
std::list<THistory> histList;//global

histList.push_back(ML::hist2[0]);//inside main()

Problem starts when I move the list inside a class.

//Problem


class ML{
    public:
    static THistory *hist2;
    static std::list<THistory> histList; //replace global list and put it inside ML class as static

}
main.cpp;
ML::hist2 = new THistory[HISTORY_BUFFER_SIZE];//global as before
////// Where to initialize the list?

ML::histList.push_back(CoreML::hist2[0]); //inside main() 
//LINK Error Undefined symbol

error LNK2001: unresolved external symbol "public: static class std::list > CoreML::histList" (?histList@CoreML@@2V?$list@VTHistory@@V?$allocator@VTHistory@@@std@@@std@@A)

I dont know how to initialize the list. any pointers will be helpful.

user437777
  • 1,423
  • 4
  • 17
  • 28

2 Answers2

0

In C++, if you declare a static member variable inside of a class, you need to put a definition of that variable somewhere. The linker error you're getting is because you've declared the variable, but you haven't defined it.

In one of the C++ source files in your program, add this line:

std::list<THistory> CoreML::histList; // Define the variable.
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
0

Within a class definition static data members are only declared. You have to define them outside the class definition. For example

class ML{
    public:
    static THistory *hist2;
    static std::list<THistory> histList; //replace global list and put it inside ML class as static

};
^^^

// before the function main

std::list<THistory> ML::histList;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335