Mail Archives: djgpp/1995/03/16/07:32:35
>
> I am trying to use a List template class and I keep getting:
>
> "undefined reference List<int>::Insert()"
> "undefined reference List<int>::First()"
>
> ...and so on for every List function that is called in my client
> program.
>
> I am using my own list implementation (not the one included with DJGPP). I
> have my List template class declaration in my header file (list.h) and the
> implementation in my list.cc file. My client program is in its separate
> file (intlist.cc).
>
> I have tried the -v switch and the above output only occurs after the linker
> is called.
>
> Any ideas? Please send responses to my e-mail address <nfluhr1 AT gl DOT umbc DOT edu>
> as I do not subscribe to the mailing list.
>
> Thanks in advance!
> Neil
>
I have just found the answer in a FAQ in the gnu.g++.help newsgroup:
I get undefined symbols when using templates
============================================
(Thanks to Jason Merrill for this section).
g++ does not automatically instantiate templates defined in other
files. Because of this, code written for cfront will often produce
undefined symbol errors when compiled with g++. You need to tell g++
which template instances you want, by explicitly instantiating them in
the file where they are defined. For instance, given the files
`templates.h':
template <class T>
class A {
public:
void f ();
T t;
};
template <class T> void g (T a);
`templates.cc':
#include "templates.h"
template <class T>
void A::f () { }
template <class T>
void g (T a) { }
main.cc:
#include "templates.h"
main ()
{
A<int> a;
a.f ();
g (a);
}
compiling everything with `g++ main.cc templates.cc' will result in
undefined symbol errors for `A<int>::f ()' and `g (A<int>)'. To fix
these errors, add the lines
template class A<int>;
template void g (A<int>);
to the bottom of `templates.cc' and recompile.
Andreas
- Raw text -