Move the definitions of the add
and greater
function templates into your number.h
.
Remember that add
and greater
aren't functions, they're function templates. To create actual functions, the compiler has to instantiate the template for specific types, such as int
, and it can only do that if it has access to the template's definition at the point where it discovers that an instance is needed.
When you compile number.cpp
, the compiler has access to the templates' definitions, but it doesn't see any code that requires a specific instance (such as number
), so it doesn't generate instances.
When you compile resolver.cpp
, the compiler sees that it needs to instantiate those templates for the int
type, but it can't since it doesn't have their definitions. So it generates "external references", basically notes telling the linker to look for those functions in some other object file.
The result is that the function templates don't get instantiated in either object file — in one because the compiler didn't know that it should, and in the other because it couldn't — so when the linker goes looking for them (to resolve those external references), it can't find them. That's why you get the error.
Moving the template function definitions into the header makes them visible to the compiler while it's compiling main.cpp
, so it's able to instantiate those functions for the int
type. Function templates typically need to be defined in header files, rather than .cpp
files, for exactly this reason.