Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

small template problem

0 views
Skip to first unread message

Peter Sprenger

unread,
Feb 28, 2009, 8:33:27 AM2/28/09
to
I have a small template problem in the CBuilder 2007.

This code is working:

template <typename T> class array
{
public:
sm_array(){}
virtual ~sm_array() {}
};


this gives no C++ but a linker error:

header:
template <typename T> class sm_array
{
public:
sm_array();
virtual ~sm_array();
};

cpp file:
template <typename T> sm_array<T>::sm_array()
{
}

template <typename T> sm_array<T>::~sm_array()
{
}

linker error:

[ILINK32 Error] Error: Unresolved external
'sm_array<int>::~sm_array<int>()' referenced from ...

[ILINK32 Error] Error: Unresolved external
'sm_array<int>::sm_array<int>()' referenced from ...


Any ideas?

Regards

Pete

Daniel T.

unread,
Feb 28, 2009, 8:54:39 AM2/28/09
to

The translation unit (.cpp file) that is using the functions is not
the same translation unit which defines the functions.

If your compiler supports the "export" keyword (very few do) then you
can use that, otherwise template code needs to be in the header.

A common way to do it is as follows:

//header:


template <typename T> class sm_array
{
public:
sm_array();
virtual ~sm_array();
};

#include "sm_array.cpp"

//implementation file:


template <typename T>
sm_array<T>::sm_array()
{ }

template <typename T>
sm_array<T>::~sm_array()
{ }

The most common way is to simply define the code in the header as your
first example shows.

As a side note, the virtual distructor surprises me. Were you planing
on having virtual member-functions in your template class?

Peter Sprenger

unread,
Feb 28, 2009, 9:08:33 AM2/28/09
to

> As a side note, the virtual distructor surprises me. Were you planing
> on having virtual member-functions in your template class?

Thanks for the fast answer. No I had not planned to use virtual member
functions in my member class. Was a copy & paste thing.

Regards Pete

red floyd

unread,
Feb 28, 2009, 3:53:28 PM2/28/09
to
0 new messages