This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]