473,769 Members | 7,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 >::TestTemplate B()
{

}

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

}

Now I want to instantiate an object of type TestTemplateB:

TestTemplateB<I Track>* track = new TestTemplateB<I Track>();

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<c lass ITrack>::TestTe mplateB<class ITrack>(void)"
(??0?$TestTempl ateB@VITrack@@@ @QAE@XZ)
Can somebody tell my what I'm doing wrong?

thanks in advance
Michael Doswald

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

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<c lass ITrack>::TestTe mplateB<class ITrack>(void)"
(??0?$TestTempl ateB@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<I Track>* track = new TestTemplateB<I Track>();

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<c lass ITrack>::TestTe mplateB<class ITrack>(void)"
(??0?$TestTempl ateB@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***********@g mx.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 >::TestTemplate B()
{

}

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

}

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

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<c lass ITrack>::TestTe mplateB<class ITrack>(void)"
(??0?$TestTempl ateB@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
3987
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 MyClass { // ... friend void MyFriendFunction (MyClass <T> * const ptrMyClass); // ...
5
1787
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
9411
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> client.cpp & client.hpp - define the class client derived from tree main.cpp - the main function instantiate client and run All these files are compiled successfully, but there's a linker error " undefined reference to `base<int>::base_run()' ". ...
14
4433
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
1959
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 the resulting functions or classes are identical, without causing any errors. Or so I thought. But a few days ago, I added a couple of new template functions to one of my personal library modules, and I got this linker error message (from...
3
3758
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 thawed it out. I built a console app using Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the header file had to be commented out. I built a console app using Borland C++ Builder 5. The linker complained of references to...
5
2539
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 determining the class type. The code is below:
5
2383
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 and trying to get the same code to work. Under Visual C++ 2005 I am seeing linker errors. The problem is in the definition of a couple of friend operators that the linker does not like for some reason. I am assuming I have a mistake in my...
3
1604
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 I get undefined error messages from the linux linker, eg: cl_foo<int>(...) not defined. Presumbly this is because theres no actual instance of cl_foo<int> created in the .o file that contains the template class. Is there a
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5309
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.