small template problem

P

Peter Sprenger

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
 
D

Daniel T.

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?

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?
 
P

Peter Sprenger

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
 
R

red floyd

Peter said:
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 ...

This is a FAQ.

http://parashift.com/c++-faq-lite/templates.html#faq-35.15
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top