g++: linking & templates

Susan Murray smurray@andrew.cmu.edu
Sat Jul 24 18:42:00 GMT 1999


Hi, this may be a error with the linker, and if it is, I'm hoping that
you can direct me to the right people.  Here's my problem:

 I have a class that uses templates, which its declaration in a .H file
and its function definitions in a .C file


.H:

template <class A>
class List {
    private:
        A data
    public:
        A foo();
};

.C:

template <class A>
A List<A>::foo() {
    return data;
}

the program compiles with no problems, but when I try to use the class
in a program, and link the two .o files together, I get the following
type of error:

undefined reference to `List<int>::foo()'

However, if I put everything into the class declaration (no .C file) it
works fine.  Its not a horrible problem, but its extremely annoying,
since the rest of the code I'm working on is all layed out in that
fashion.  Anyhow, any sort of information you could give me on this
would be quite helpful, and if you have any questions about this
phenomena I might be able to answer to help you out, just let me know.

also, here are the version numbers of all the programs i'm using:

g++: egcs-2.91.66
ld:  GNU ld 2.9.1
as:  GNU assembler 2.9.1

I also want to let you guys know that this is the only problem I've ever

had using g++.  You put out a quality product!

craig
soules@andrew.cmu.edu




More information about the Gcc-bugs mailing list