First time using templates and multiple errors occurred

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;
}


The header file if needed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef inpalprime_hpp
#define inpalprime_hpp

#include <vector>
#include <string>


namespace inpal
{
    template <class T> T max_prime(T n);
    template <class T> T count_primes(T n);
    template <class T> double prime_density(T h);
    template <class T> bool prime_test(T p);
    template <class T> bool twin_test(T p);
    template <class T> bool cousin_test(T p);
    template <class T> bool sexy_test(T p);
    template <class T> T max_palprime(T n);
    template <class T> T max_factor(T f);
    template <class T> T count_factors(T f);
    
    template <class T> std::vector<bool> prime_sieve(T m);
    template <class T> std::vector<T> factorizer(T f);
    template <class T> bool pal_test(T n);
}



#endif /* inpalprime_hpp */ 


Test with main():
1
2
3
4
int main()
{
    std::cout << inpal::max_prime<int>(2910091) << std::endl;
}


What do I need to do to fix this errors and is there a better way to use templates in this case?
Last edited on
Did you include your hpp file in your cpp file where your template func are defined?

I see that prime_sieve() is also involved. So you might want to as well as post the func on here too.
Yes I did include it, here is prime_sieve():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
template <class T> std::vector<bool> inpal::prime_sieve(T m)
{
    std::vector<bool> p_test(m+1, false);
    
    //defines square root of m
    T root = ceil(sqrt(m));
    
    //sieve axioms
    for(T x=1; x<=root; x++)
    {
        for(T y=1; y<=root; y++)
        {
            T i= (4*x*x)+(y*y);
            if (i<=m && (i%12==1 || i%12==5))
            {
                p_test[i].flip();
            }
            
            i=(3*x*x)+(y*y);
            if(i<=m && i%12==7)
            {
                p_test[i].flip();
            }
            
            i=(3*x*x)-(y*y);
            if(x>y && i<=m && i%12==11)
            {
                p_test[i].flip();
            }
        }
    }
    
    //marks 2,3,5 and 7 as prime numbers
    p_test[2]=p_test[3]=p_test[5]=p_test[7]=true;
    
    //marks all multiples of primes as non primes
    for(T r=5; r<=root; r++)
    {
        if(p_test[r])
        {
            for(T j=r*r; j<=m; j+=r*r)
            {
                p_test[j]=false;
            }
        }
    }
    
    return p_test;
}
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.

Topic archived. No new replies allowed.