Consider the code below:
header.h
:
#pragma once
template<class T>
void f(T const& val);
header.cpp
:
#include "header.h"
template<class T>
void f(T const& val)
{
}
main.cpp
:
int main()
{
f(5);
}
The compiler output is a link error which states:
error LNK2019: unresolved external symbol "void __cdecl f<int>(int const &)" (??$f@H@@YAXABH@Z) referenced in function _main
I know that if I place the body of template function in header.h
, my problem will be solved. But why? Why linker can find the body of non template functions but cannot find the body of template functions?