This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
RE: linking templates
- From: "Smith, Jacob N." <jnsmith at utmb dot edu>
- To: 'Paulo Cortes' <cortes at uiuc dot edu>, gcc-help at gcc dot gnu dot org
- Date: Fri, 20 Jun 2003 07:46:25 -0500
- Subject: RE: linking templates
> I get
> undefined reference to `Array<float>::Array()' for example.
I'm not sure of the layout of your file, but with templates simply including
the header file with the declarations is not enough. You must specifically
include the definitions. The following would produce a similar result to
what you have:
***** foo.h *********
template < typename bar >
class foo {
public: foo(int);
};
***** foo.cpp ******
#include <iostream>
#include "foo.h"
template < typename bar >
foo::foo(int x) {
std::cout << "Including source is a Good Thing(TM) when asking
questions: " << x << std::endl;
}
*********************
Happily compiles, but as a library, calls like "foo<int> whee(3);" won't
work.
One solution is to rewrite foo.h like this:
***** foo_superior.h *****
template < typename bar >
class foo_awesome {
public: foo_awesome(int x) { std::cout << "No errors! " << x << std::endl; }
};
-j.