This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Template ctor/dtor undefined at link
- To: gcc at gcc dot gnu dot org
- Subject: Template ctor/dtor undefined at link
- From: Robert Boehne <rboehne at ricardo-us dot com>
- Date: Mon, 01 Oct 2001 14:21:41 -0500
- Organization: Ricardo Software
Hello,
I can't determine whether this particular problem is a compiler
problem or a (most likely) programmer error. I have a template
class with a constructor that takes a pointer to the template
argument, gcc 3.0.1 x86 Linux and Aix 4.3 as well as egcs-2.91.66
all have unresloved symbols at link time, some other compilers don't.
g++ -c -o main.o main.cpp
g++ -c -o B.o B.cpp
g++ -c -o T.o T.cpp
g++ -o test main.o B.o T.o
main.o: In function `main':
main.o(.text+0x97): undefined reference to `C<B>::C(B*) '
main.o(.text+0x12f): undefined reference to `C<B>::~C() '
collect2: ld returned 1 exit status
file B.hpp:
#include <iostream.h>
class B {
public:
~B();
B();
private:
double x;
};
file B.cpp:
#include "B.hpp"
B::~B()
{
x = 0.5;
cout << "Destroy B x= " << x << endl;
}
B::B()
{
x = 100.5;
cout << "Construct B x= " << x << endl;
}
file T.hpp:
#ifndef T_HPP
# define T_HPP
#include <iostream.h>
template<class Tp1> class C {
public:
~C();
C(Tp1 *parent);
private:
Tp1 * thispointer;
};
#endif // T_HPP
file T.cpp:
#include "T.hpp"
template<class Tp1> C<Tp1>::~C()
{
cout << "destructor of C<Tp1>" << endl;
}
template<class Tp1> C<Tp1>::C(Tp1 *parent)
{
thispointer = parent;
cout << "constructor of C<Tp1>" << endl;
}
file main.cpp:
#include "B.hpp"
#include "T.hpp"
int main(int argc, char* argv[])
{
C<B> *data;
B *daParent;
daParent = new B();
data = new C<B>::C<B>(daParent);
delete daParent;
delete data;
return(0);
}
--
Robert Boehne Software Engineer
Ricardo Software Chicago Technical Center
TEL: (630)789-0003 x. 238
FAX: (630)789-0127
email: rboehne@ricardo-us.com