0

Suppose I'm writing a DLL library. This library contains a single template and a class.

// Enum.h (Template part of library)
template <typename Type> class Enum
{
private:
    Enum (void); // User implemented
    static std::map<const char*, Type> mMap;

public:
    static int size() { return 0; }
};

// Misc.h (Class part of library)
enum MiscEnum { M1, M2, M3, M4 };
struct LIB_EXPORT Misc { Misc(); };

// Misc.cc (Impl)

// **Errors Below**
std::map<const char*, MiscEnum> Enum<MiscEnum>::mMap;
Enum<MiscEnum>::Enum()
{
    mMap["M1"] = M1;
    mMap["M2"] = M2;
    mMap["M3"] = M3;
    mMap["M4"] = M4;
}
// **Errors Above**

Misc::Misc()
{
    std::cout << Enum<MiscEnum>::Size() << std::endl;
}

// Main.cc (App using library)
enum MainEnum { E1, E2, E3 };

std::map<const char*, MainEnum> Enum<MainEnum>::mMap;
Enum<MainEnum>::Enum()
{
    mMap["E1"] = E1;
    mMap["E2"] = E2;
    mMap["E3"] = E3;
}

void main()
{
    std::cout << Enum<MiscEnum>::Size() << std::endl;
    std::cout << Enum<MainEnum>::Size() << std::endl;
}

I'm getting a linker error when using Enum inside the library. I understand the issue and that there needs to be a LIB_EXPORT type of declaration somewhere but I'm not quite sure where.

Dave
  • 7,283
  • 12
  • 55
  • 101
  • 1
    Related: http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – SleuthEye Sep 06 '14 at 23:28
  • "I'm getting a linker error.." Twice this is mentioned in this post (the title and the closing paragraph). Should we pretend we were told what the error *was* ? Please provide complete, *verbatim* error messaging if your post is a question about addressing an unwanted error (is there another kind?). – WhozCraig Sep 07 '14 at 10:22
  • `Enum::Enum()` That wouldn't compile. You seem to be trying to implement a specialization, but the syntax is wrong. In any case, you are not actually exporting any purported specialization of any member of `Enum` from your DLL. – Igor Tandetnik Sep 07 '14 at 16:16

0 Answers0