Move the add and greater function template definitions to your number.h .
Remember that add and greater are not functions; they are functional patterns. To create the actual functions, the compiler must create an instance of the template for certain types, for example int , and it can only do this if it has access to the template definition at the point where it detects that the instance is needed.
When you compile number.cpp , the compiler has access to the template definitions, but it does not see any code requiring a specific instance (for example, number<int> ), so it does not generate instances.
When you compile resolver.cpp , the compiler sees that it needs to create these templates for the int type, but it cannot, because it does not have its own definitions. Thus, it generates "external links", basically, it notes that the linker is looking for these functions in some other object file.
As a result, function templates do not receive an instance in any object file - in one because the compiler did not know what it should, and in the other because it could not - therefore, when the linker searches for them (to allow these external links), they can't find them. That is why you get an error.
Moving the definitions of the template functions to the header makes them visible to the compiler when compiling main.cpp , so it can create these functions for the int type. For this reason, function templates should usually be defined in header files, not in .cpp files.
Wyzard
source share