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

Recommended Answers

All 5 Replies

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

/Foo.h
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.

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:

Foo::m_mSessionId2CCRNum2DefaultBearerId();

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.

EDIT:

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

commented: Good - give credit where credit is due! :-) +12

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.