I have a approximate following class

class ClassA : public ClassB
    void                        HandleMessages(const u8 *pP, const u16 nMsgBodyLen);

    static std::map<std::pair<u32, u32>, CreateSessInfo> m_mSessionId2CCRNum2DefaultBearerId;


When trying to access the map from the HandleMessage, I am getting the linker error, saying the
undefined reference to m_mSessionId2CCRNum2DefaultBearerId;

Could some c++ experts help me

3 Years
Discussion Span
Last Post by NathanOliver

You have to initialize the static member outside of the class.

class Foo
    static int bar;

// Foo.cpp
int Foo::bar = 1;

If the initialization is in the header file then each file that includes the header file will have a definition of the static member. Thus during the link phase you will get linker errors as the code to initialize the variable will be defined in multiple source files.

Edited by NathanOliver


You have not declared the method prototype correctly in your class.

static std::map<std::pair<u32, u32>, CreateSessInfo> m_mSessionId2CCRNum2DefaultBearerId;

Shoudl be:

static std::map<std::pair<u32, u32>, CreateSessInfo> m_mSessionId2CCRNum2DefaultBearerId()
    // Logic here

    return map; 


Then you can access it as:


Here, I did a similar example: http://ideone.com/NelSt9 no logic is there, but, I hope it gives you an understanding of where you went wrong.


Just realised what you were attempting, so, @NathanOliver answer is more accurate.

Edited by phorce

Votes + Comments
Good - give credit where credit is due! :-)

Thanks a lot Nathan and Phorce. This issue is solved now


It was my mistake, just to reconfirm that now issue is solved, after defining the map in the .cpp file.

Edited by pdk123


You have to initialize the map. Take a look at this post As how you can do that with or without C++11 support.

Edited by NathanOliver

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.