Undefined Reference to Template Function

undefined reference to template function

The implementation of a non-specialized template must be visible to a translation unit that uses it.

The compiler must be able to see the implementation in order to generate code for all specializations in your code.

This can be achieved in two ways:

1) Move the implementation inside the header.

2) If you want to keep it separate, move it into a different header which you include in your original header:

util.h

namespace Util
{
template<class T>
QString convert2QString(T type , int digits=0);
}
#include "util_impl.h"

util_impl.h

namespace Util
{
template<class T>
QString convert2QString(T type, int digits=0)
{
using std::string;

string temp = (boost::format("%1") % type).str();

return QString::fromStdString(temp);
}
}

C++ undefined reference to template method

Template classes/functions must be defined in the header file, not in .cpp

see: Why can templates only be implemented in the header file?

C++ class template undefined reference to function

Your class is named wrong. Your class is named cai where all your functions belong to a class named number: http://ideone.com/ZayX0c

One more thing.. you cannot have templates in the .cpp file. Template functions/defintions go in the header along with the class declaration. This is the reason for your undefined function error. Non-template functions go in the .cpp.

#include <cstdio>
#include <cstdlib>

template <class T>
class number {
public:
T x;
T y;

number (int a, int b){
x=a; y=b;}
int add (T&);
T greater ();
};

template <class T>
int number<T>::add (T& rezAdd){
rezAdd = x+y;
return 1;
}

template <class T>
T number<T>::greater (){
return x>y? x : y;
}

int main (int argc, char **argv) {
int aux;
number<int> c(3,5);

c.add(aux);
printf ("number added [%d]\n", c.add(aux));
printf ("greater number: [%d]\n", c.greater());

return 0;
}

C++ undefined reference to template class method

Typically you want your template methods in the header, so they are compiled when needed. In case you really want to hide it in the implementation file, you have to explicitly instantiate the template in Graph.cpp like

template class  Graph<string>;

Since you have to do that for every type T you intend to use with Graph<T>, the point of the template class is somewhat defeated and you better put everything into the header

Undefined reference error for template method

Templated code implementation should never be in a .cpp file: your compiler has to see them at the same time as it sees the code that calls them (unless you use explicit instantiation to generate the templated object code, but even then .cpp is the wrong file type to use).

What you need to do is move the implementation to either the header file, or to a file such as VAConfig.t.hpp, and then #include "VAConfig.t.hpp" whenever you use any templated member functions.

Undefined reference to template member function

Templates need to only be defined within header files, this is why you are getting an undefined reference. I was able to run this successfully once I moved the source template code file into the header file.

Error message undefined reference to template function passed as template parameter

It seems the problem is a gcc error: the code compiles and links with clang, icc, and the EDG frontend. A potential work-around not changing any of the uses would be the use of a class template identity instead of a function:

template<int I>
struct identity {
operator int() { return I; }
};

template<typename fn>
class Base {
public:
int f() {
return fn();
}
};

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


Related Topics



Leave a reply



Submit