简体   繁体   English

C ++中的静态地图初始化

[英]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. 不幸的是,我仍然有一个似乎无法破解的错误-尽管我做了15年前的很多c ++开发。

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): 这是到目前为止的代码(css代码似乎无法很好地渲染它):

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 . 您缺少table的定义。 Somewhere in your code it should say: 在代码中的某个地方应该说:

map<RegionCodeEnum, DeviceCodeEnum> LookupTable::table;

just as you did for constructor cons . 就像您对constructor cons

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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