C++ Template, Linking Error

Template issue causes linker error (C++)

You have to have your template definitions available at the calling site. That means no .cpp files.

The reason is templates cannot be compiled. Think of functions as cookies, and the compiler is an oven.

Templates are only a cookie cutter, because they don't know what type of cookie they are. It only tells the compiler how to make the function when given a type, but in itself, it can't be used because there is no concrete type being operated on. You can't cook a cookie cutter. Only when you have the tasty cookie dough ready (i.e., given the compiler the dough [type])) can you cut the cookie and cook it.

Likewise, only when you actually use the template with a certain type can the compiler generate the actual function, and compile it. It can't do this, however, if the template definition is missing. You have to move it into the header file, so the caller of the function can make the cookie.

C++ template, linking error

Template functions, including member functions, must be written entirely in the header files. This means that if you have a template class, its implementation must be entirely in a header file. This is because the compiler needs to have access to the entire template definition (not just the signature) in order to generate code for each instantiation of the template.

Linker error when using a template class?

Due to a weirdness in C++'s compilation model, you cannot separate out .h and .cpp files very cleanly for template classes. Specifically, any translation unit (C++ source file) that wants to use a template class has to have access to the entire template definition. This is a strange quirk of the language, but unfortunately it's here to stay.

One option is to put the implementation up in the header file rather than in the source, then to not have a .cpp file at all. For example, you might have this header:

#pragma once
#ifndef hijo_h
#define hijo_h

template <class A>
class hijo
{
public:
hijo(void);
~hijo(void);
};

/* * * * Implementation Below This Point * * * */

template <class A>
hijo<A>::hijo(void)
{
}
template <class A>
hijo<A>::~hijo(void)
{
}

#endif

Hope this helps!

C++ template, linking stage undefined reference error

Make your class abstract (by adding = 0), something like:

template <class T, class R>
class Gathering {
public:
virtual R gather(const T&) = 0;
};

Explicit instanciation of template linker error

Since you are using a nonstandard extension, .tpp, you may want to tell the compiler that you are working with C++ code, g++ -x c++.

C++ linker error when calling template function that is defined in header

The problem seems to be that you wrote

std::shared_ptr<T> ptr(T);

instead of

std::shared_ptr<T> ptr(value);

how template instantiation doesn't lead to linking error

The linker doesn't complain because the standard says so:

[basic.def.odr]/13

There can be more than one definition of a

...

— inline function or variable ...

— templated entity ...

In this regard, the linker handles them as if they were inline.

Note that they're not actually inline (for the purposes of optimizations). You can mark them as inline yourself, hinting the compiler that they should be inlined.



Related Topics



Leave a reply



Submit