0

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?

K-ballo
  • 80,396
  • 20
  • 159
  • 169
IUnknown
  • 2,596
  • 4
  • 35
  • 52
  • That's not a compiler error, that's a linker error. Add `map LookupTable::table;` to the code. In C++11 you can spare yourself the entire thing and just use a brace-initializer. – Kerrek SB Nov 19 '11 at 15:01
  • 1
    See the answers to: [this](http://stackoverflow.com/q/7092765/485561), [this](http://stackoverflow.com/q/272900/485561), [this](http://stackoverflow.com/q/7091712/485561), [this](http://stackoverflow.com/q/5603101/485561) or [this](http://stackoverflow.com/q/2145331/485561) – Mankarse Nov 19 '11 at 15:15

1 Answers1

6

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.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
  • Thanks - makes sense. It compiles now but get a runtime error: Unhandled exception at 0x773415ee in Refactor01.exe: 0xC0000005: Access violation reading location 0x00000004. The following line in xtree: iterator lower_bound(const key_type& _Keyval) { // find leftmost node not less than _Keyval in mutable tree return (iterator(_Lbound(_Keyval), this)); } – IUnknown Nov 19 '11 at 15:41
  • 1
    Put this line **above** the definition of `LookupTable::cons`. – Kerrek SB Nov 19 '11 at 15:48
  • Running into a new challenge now. As soon as I am using the LookupTable from more than one class I get this error: Error 1 error LNK2005: "public: static enum DeviceCodeEnum__cdecl LookupTable::GetDeviceFromRegion(enum RegionCodeEnum)" (?GetDeviceFromRegion@LookupTable@@SA?AW4RegionCodeEnum@@W42@@Z) already defined in main.obj C:\_dev\temp\test\ValueHandler.obj Refactor01 – IUnknown Nov 19 '11 at 16:41