473,394 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Template inheritance and linker error

Hello group,

I have a problem with template classes and inheritance. I've searched
on the internet to find a solution but all the examples look the same
as my code (as far as I can tell) and I can't find my mistake (maybe
there's something wrong with my eyes).

I've two classes:

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

};

template< class T >
class TestTemplateB : public ITestTemplateA< T >
{
public:
TestTemplateB();
virtual ~TestTemplateB();

};

and the implementation of TestTemplateB:

template < class T>
TestTemplateB<T>::TestTemplateB()
{

}

template< class T >
TestTemplateB<T>::~TestTemplateB()
{

}

Now I want to instantiate an object of type TestTemplateB:

TestTemplateB<ITrack>* track = new TestTemplateB<ITrack>();

Where ITrack is another class, but I think that the source-code to that
class isn't important (?). This compiles but the linker shows an error:
error LNK2001: unresolved external symbol "public: __thiscall
TestTemplateB<class ITrack>::TestTemplateB<class ITrack>(void)"
(??0?$TestTemplateB@VITrack@@@@QAE@XZ)
Can somebody tell my what I'm doing wrong?

thanks in advance
Michael Doswald

Nov 13 '06 #1
3 2647
Now I want to instantiate an object of type TestTemplateB:
>
TestTemplateB<ITrack>* track = new TestTemplateB<ITrack>();

Where ITrack is another class, but I think that the source-code to that
class isn't important (?). This compiles but the linker shows an error:
error LNK2001: unresolved external symbol "public: __thiscall
TestTemplateB<class ITrack>::TestTemplateB<class ITrack>(void)"
(??0?$TestTemplateB@VITrack@@@@QAE@XZ)
Can somebody tell my what I'm doing wrong?

It is important. When the compiler can't see the source code of the
class template, it assumes the instantiation is elsewhere. When it sees
the source code elsewhere it wouldn't know what T is used to instantiate
it, so it won't emit code for it. In fact, the compiler may not even
know what ITrack is when it is compiling the class templates, let alone
knowing that ITrack is used to instantiate the templates. You ended up
with linker complaining about unresolved reference.

To formal solution is to export your class template. But sorry to say,
this feature isn't implemented on most systems so you will have to check
yours (for example, Visual C++ compiler doesn't support that.)

The other solution is to put all the implementation of the class
template in the header file so the compiler sees it when it is asked to
instantiate. I believe C++ linkers, in this case, are able to strip away
redundant code. This also helps effective inlining, too.

>
thanks in advance
Michael Doswald
Ben
Nov 13 '06 #2
Now I want to instantiate an object of type TestTemplateB:

TestTemplateB<ITrack>* track = new TestTemplateB<ITrack>();

Where ITrack is another class, but I think that the source-code to that
class isn't important (?). This compiles but the linker shows an error:

error LNK2001: unresolved external symbol "public: __thiscall
TestTemplateB<class ITrack>::TestTemplateB<class ITrack>(void)"
(??0?$TestTemplateB@VITrack@@@@QAE@XZ)

Can somebody tell my what I'm doing wrong?
To formal solution is to export your class template. But sorry to say,
this feature isn't implemented on most systems so you will have to check
yours (for example, Visual C++ compiler doesn't support that.)
As a matter of fact, I am using Visual C++ :)

The other solution is to put all the implementation of the class
template in the header file so the compiler sees it when it is asked to
instantiate.
I've tried this solution and it works. Thanks for your help!

Michael

Nov 13 '06 #3

li***********@gmx.net wrote:
Hello group,

I have a problem with template classes and inheritance. I've searched
on the internet to find a solution but all the examples look the same
as my code (as far as I can tell) and I can't find my mistake (maybe
there's something wrong with my eyes).

I've two classes:

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

};

template< class T >
class TestTemplateB : public ITestTemplateA< T >
{
public:
TestTemplateB();
virtual ~TestTemplateB();

};

and the implementation of TestTemplateB:
where is this implementation, in which file?
>
template < class T>
TestTemplateB<T>::TestTemplateB()
{

}

template< class T >
TestTemplateB<T>::~TestTemplateB()
{

}

Now I want to instantiate an object of type TestTemplateB:
does compiler know what ITrack is at this point?
TestTemplateB<ITrack>* track = new TestTemplateB<ITrack>();

Where ITrack is another class, but I think that the source-code to that
class isn't important (?).
It is if the TestTemplateB/A use it in the constructor
or have it in the body as a member. you would
get a compiler error thou ...
This compiles but the linker shows an error:
error LNK2001: unresolved external symbol "public: __thiscall
TestTemplateB<class ITrack>::TestTemplateB<class ITrack>(void)"
(??0?$TestTemplateB@VITrack@@@@QAE@XZ)
The linker can't see the implementaiton of
the constructtor, again, where is it?
Did you put it in cpp file?
Can somebody tell my what I'm doing wrong?

thanks in advance
Michael Doswald
Nov 13 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ruben Campos | last post by:
I have a problem with a template function that is declared as a friend of a template class. I'll first show the exact problem with source code: // MyClass.hpp template <typename T> class...
5
by: tuko | last post by:
The following snipet gives a linker error. I don't get it... template<class T> class tran { public: public: private: }; template<class T> class matrix {
2
by: haplotype | last post by:
I have designed a package with several files, which is listed bellow base.cpp & base.hpp - define the template class base tree.cpp & tree.hpp - define the class tree derived from base<int>...
14
by: Murkland | last post by:
Hi. I have a problem with a template class that I'm trying to inherit from. The code for the base class is: template <class T> class BaseClass { public: BaseClass(); ~BaseClass();
0
by: Robbie Hatley | last post by:
I'd always thougth that a C++ compiler/linker should be able to instantiate a template in mulitple places (say, in two different translation units), even using the same template parameters so that...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
5
by: Wayne Shu | last post by:
Hi, guys I am reading Vandevoorde and Josuttis 's "C++ Template The Complete Guide" these days. When I read the chapter 15: Traits and Policy classes. I copy the code in 15.2.2 that use to...
5
by: 2beagles | last post by:
I have a template for a three dimensional array that I have been working on using Visual C++ 6.0. Under version 6 this code worked fine. However, last night I downloaded Visual C++ 2005 Express and...
3
by: Boltar | last post by:
Hi I have a template class thats defined in a header file and implemented in a .cpp module that is compiled to a .o file. No problems there. But when I try to use this class from another .o file...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.