简体   繁体   中英

static map initialization in c++

There are a number of similar questions, in fact I composed the following code from several other posts. Unfortunately I still have one error I can't seem to crack - and although I did a lot of c++ development that was 15 years ago.

I want to make a simple static look-up table using a map.

Here is the code so far (the code css seems to not render it very well):

enum RegionCodeEnum
{
    One,
    Two,
    Three
};

enum DeviceCodeEnum
{
    AAA,
    BBB,
    CCC
};

class LookupTable
{
    friend class constructor;

    struct constructor 
    {
        constructor() 
        { 
            table[One] = AAA;
            table[Two] = AAA;
            table[Three] = CCC;
        }
    };

    static constructor cons;

public:
    LookupTable(void);

    static DeviceCodeEnum GetDeviceFromRegion(RegionCodeEnum RegionCode);

private:
    static map<RegionCodeEnum, DeviceCodeEnum> table;
};

LookupTable::constructor LookupTable::cons;

LookupTable::LookupTable(void)
{

}

DeviceCodeEnum LookupTable::GetDeviceFromRegion(RegionCodeEnum RegionCode)
{
    return table[RegionCode];
}

From else where in the code I have this code:

DeviceCodeEnum code= LookupTable::GetDeviceFromRegion(One);

The compile error I get is:

error LNK2001: unresolved external symbol "private: static class std::map<enum RegionCodeEnum,enum DeviceCodeEnum,struct std::less<enum DeviceCodeEnum>,class std::allocator<struct std::pair<enum RegionCodeEnum const ,enum DeviceCodeEnum> > > LookupTable::table" (?table@LookupTable@@0V?$map@W4RegionCodeEnum@@W41@U?$less@W4DeviceCodeEnum@@@std@@V?$allocator@U?$pair@$$CBW4DeviceCodeEnum@@W41@@std@@@3@@std@@A)   C:\_dev\temp\test\main.obj  Refactor01

Any thoughts?

You are missing the definition for table . Somewhere in your code it should say:

map<RegionCodeEnum, DeviceCodeEnum> LookupTable::table;

just as you did for constructor cons .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM