W
willo
All,
I'm having problems getting base-class template symbols to link
correctly.
My unit-test files are included below. I defined a templated Base-
Class 'A', and a templated Derived-Class 'B', only defining the
constructor and destructor.
I'm using 'g++-4.2', and get errors like:
willo:~/tmp$ g++ main.o B.o A.o -o main
B.o: In function `B<int>::B()':
B.cpp.text._ZN1BIiEC1Ev[B<int>::B()]+0x11): undefined reference to
`A<int>::A()'
B.o: In function `B<int>::~B()':
B.cpp.text._ZN1BIiED1Ev[B<int>::~B()]+0x11): undefined reference to
`A<int>::~A()'
This is despite the fact that I'm linking in another object file that
defines these symbols:
willo:~/tmp/$ nm --demangle A.o
0000000000000000 t instantiate_templated_class()
0000000000000000 W A<int>::A()
0000000000000000 W A<int>::~A()
U __gxx_personality_v0
Interestingly, if I remove the template parameter from Class A and B,
this error does not occur.
So, my question is, what am I doing wrong, or is what I'm trying to do
possible?
-- Charles Wilcox
willo:~/tmp$ cat A.h
#ifndef A_HEADER
#define A_HEADER
template< typename T >
class A
{
public: // methods
A();
~A();
};
#endif // ifndef A_HEADER
willo:~/tmp$ cat A.cpp
#include "A.h"
template< typename T >
A< T >::A() {}
template< typename T >
A< T >::~A() {}
static void instantiate_templated_class()
{
A< int > a;
}
willo:~/tmp$ cat B.h
#ifndef B_HEADER
#define B_HEADER
#include "A.h"
template< typename T >
class B : public A< T >
{
public: // methods
B();
~B();
};
#endif // ifndef B_HEADER
willo:~/tmp$ cat B.cpp
#include "B.h"
template< typename T >
B< T >::B() {}
template< typename T >
B< T >::~B() {}
static void instantiate_templated_class()
{
B< int > b;
}
willo:~/tmp$ cat main.cpp
#include "B.h"
int main( int argc, char* argv )
{
B< int > b;
return 0;
}
I'm having problems getting base-class template symbols to link
correctly.
My unit-test files are included below. I defined a templated Base-
Class 'A', and a templated Derived-Class 'B', only defining the
constructor and destructor.
I'm using 'g++-4.2', and get errors like:
willo:~/tmp$ g++ main.o B.o A.o -o main
B.o: In function `B<int>::B()':
B.cpp.text._ZN1BIiEC1Ev[B<int>::B()]+0x11): undefined reference to
`A<int>::A()'
B.o: In function `B<int>::~B()':
B.cpp.text._ZN1BIiED1Ev[B<int>::~B()]+0x11): undefined reference to
`A<int>::~A()'
This is despite the fact that I'm linking in another object file that
defines these symbols:
willo:~/tmp/$ nm --demangle A.o
0000000000000000 t instantiate_templated_class()
0000000000000000 W A<int>::A()
0000000000000000 W A<int>::~A()
U __gxx_personality_v0
Interestingly, if I remove the template parameter from Class A and B,
this error does not occur.
So, my question is, what am I doing wrong, or is what I'm trying to do
possible?
-- Charles Wilcox
willo:~/tmp$ cat A.h
#ifndef A_HEADER
#define A_HEADER
template< typename T >
class A
{
public: // methods
A();
~A();
};
#endif // ifndef A_HEADER
willo:~/tmp$ cat A.cpp
#include "A.h"
template< typename T >
A< T >::A() {}
template< typename T >
A< T >::~A() {}
static void instantiate_templated_class()
{
A< int > a;
}
willo:~/tmp$ cat B.h
#ifndef B_HEADER
#define B_HEADER
#include "A.h"
template< typename T >
class B : public A< T >
{
public: // methods
B();
~B();
};
#endif // ifndef B_HEADER
willo:~/tmp$ cat B.cpp
#include "B.h"
template< typename T >
B< T >::B() {}
template< typename T >
B< T >::~B() {}
static void instantiate_templated_class()
{
B< int > b;
}
willo:~/tmp$ cat main.cpp
#include "B.h"
int main( int argc, char* argv )
{
B< int > b;
return 0;
}