linking templates
Smith, Jacob N.
jnsmith@utmb.edu
Fri Jun 20 12:46:00 GMT 2003
> 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.
More information about the Gcc-help
mailing list