Search code examples
c++visual-studiotemplateslnk2019

LNK2019 Unresolved External Symbol, Can't figure out why?


apologies in advance as this is most likely my own impotence to find the error and simply overlooking the answer.
Anyway; when invoking XEngine::MapConstBufferData in my entry point I run into LNK2019, I'm quite clueless as to why but believe the error to lay within the fact that it is template function, all help is highly appreciated!

XTypes.h

struct TRANSLATE2D
{
    FLOAT OffsetX, OffsetY;
};

XEngine.h

template <class BufferType>
VOID MapConstBufferData(ComPtr<ID3D11Buffer> Buffer, BufferType BufferData, UINT Size);

XEngine.cpp

template <class BufferType>
VOID XEngine::MapConstBufferData(ComPtr<ID3D11Buffer> Buffer, BufferType BufferData, UINT Size)
{
    Contents are irrelevant. 
}

EntryPoint.cpp

INT MAIN
{
    XTYPES::TRANSLATE2D Translate{};
    Engine.MapConstBufferData<XTYPES::TRANSLATE2D>(Engine.GetConstBuffer(), Translate, sizeof(XTYPES::TRANSLATE2D));
    // ^ LNK2019: Unresolved External 

    Contents are irrelevant. 

}

Solution

  • templates require either header-file only implementation, or explicit instantiation.

    The compiler can't code the template when it sees it in EntryPoint.cpp as it doesn't have the rules.

    The compiler doesn't realize it needs it when it sees it in XEngine.cpp