Undefined symbol on a template operator overloading function

I have this function declaration:

template<class T>
a::A& a::A::operator<<(T out) {
    std::cout << out;
    return (*this);
}

and this function definition:

namespace a {
    ...
    class A {
        ...
        template<class T> A& operator<<(T);

And I call it as:

a::A b;
b << 1;

and this is the Makefile:

app: main.o A.o
    g++ main.o A.o -o app

main.o: main.cpp
    g++ -c main.cpp

A.o: A.cpp
    g++ -c A.cpp

and it gives me:

Undefined symbols: a::A& a::A::operator<< <int>(int)

why is that?


Solution 1:

The function template will be turned into an actual function at compile time, once the type represented by T (that is, int in your case) is actually known. However, this is not the case before main.cpp is compiled. At the time when A.cpp is compiled, the template function is not instantiated into an actual function, therefore the object file generated doesn't include the binary version of the function.

There are two ways to solve this.

  1. Include the function definition in your header file. That is, make

    template<class T>
    a::A& a::A::operator<<(T out) {
        std::cout << out;
        return (*this);
    }
    

    a part of the header file, and remove the function definition from the .cpp file.

    The effect of this is that any .cpp file that includes this header will be able to use any instantiation of the template, i.e. for any value of T.

  2. Alternatively, include an explicit template instantiation statement in A.cpp:

    template a::A& a::A::operator<<(int out);
    

    This will cause the compiler to actually instantiate the template when A.cpp is compiled, and to include the compiled function in the object file. Hence the linker can find it when linking main.o and A.o together, and all is fine. The disadvantage is that it will only work for the specific types (in this case, only int) that you provided explicit instantiations for.