I am trying to incorporate templates into a library I am building. While doing this I was exposed to the following problems:
-Undefined symbols for architecture x86_64:
"int inpal::max_prime<int>(int)", referenced from: main()
-clang: error: linker command failed with exit code 1 (use -v to see invocation)
Here is an example of a function I wrote:
1 2 3 4 5 6 7
template <class T> T inpal::max_prime(T n)
{
auto primes = prime_sieve(n);
auto it = std::find(primes.rbegin(), primes.rend(), true);
return primes.size()-std::distance(primes.rbegin(), it)-1;
}
Don't forget that with templates both the declaration and the definitions must be in the same compilation unit. This usually means that both the class declaration and definition (implementation) are contained within the header file.
Did you include your hpp file in your cpp file where your template func are defined?
With templates, if you split them into implementation (.cpp) and declaration (.h) you would #include the implementation into the bottom of the header file.