473,785 Members | 3,349 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to make template classes in .o files work?

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
way of getting the linker to use a bit of brains and create it on the
fly or do I have to just put the entire template class implementation
in a header file and #include it everywhere I need it (which seems a
bit archaic)?

Thanks for any help

B2003
Dec 26 '07 #1
3 1604
Boltar wrote:
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.
That's the reason.
Is there a way of getting the linker to use a bit of brains and create it
on the fly
No. The linker would have to see the source code and provide it to the
compiler somehow to let it generate the code. I don't know if any toolchain
does that. If by "the linux linker", you mean the GNU linker, then no, it
can't do that.
or do I have to just put the entire template class implementation
in a header file and #include it everywhere I need it (which seems a
bit archaic)?
Well, you can still put the implementation in a .cpp file and then #include
that at the end of your header. Another alternative, if the set of template
instances is known in the place where the template is defined, you can use
explicit template instantiation to make the compiler generate those
template instances that you need.

Dec 26 '07 #2
On 26 Dec, 11:54, Rolf Magnus <ramag...@t-online.dewrote:
Well, you can still put the implementation in a .cpp file and then #include
that at the end of your header. Another alternative, if the set of template
instances is known in the place where the template is defined, you can use
explicit template instantiation to make the compiler generate those
template instances that you need.
Good idea, I'll try that. Thanks.

B2003

Dec 26 '07 #3
On 2007-12-26 07:14:13 -0600, Boltar <bo********@yah oo.co.uksaid:
On 26 Dec, 11:54, Rolf Magnus <ramag...@t-online.dewrote:
>Well, you can still put the implementation in a .cpp file and then #include
that at the end of your header. Another alternative, if the set of template
instances is known in the place where the template is defined, you can use
explicit template instantiation to make the compiler generate those
template instances that you need.

Good idea, I'll try that. Thanks.
Note that explicitly instantiating those templates mean that your
library user's code can't instantiate other instances for themselves.
This may or may not be what you want.

-dr

Dec 26 '07 #4

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

Similar topics

1
3344
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me to turn a previously made String class that deals with char's into a templated String class that uses the template parameter C instead of char. I thought it would be fairly simple to do this exercise, but I encoutered many errors for my...
0
1645
by: Chris F Clark | last post by:
In our C++ project we have some internal bug reporting macros that we use to get useful information when the program does something unexpected. Essentially at the point of the error, we invoke an internal interactive debugger that knows the classes within our system and allow us to walk around the objects that exist at the time of the fault. It mostly works fairly well. That catch being that we have to hand implement some of the code...
2
3191
by: Lokicer | last post by:
Hi! I am a newbie in C++, it seems compiler must see the source code of template classes(with .h files and .cpp files£©when compile. I want to prevent others from getting .cpp files. How should i do? Thanks!
10
4846
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
12
2625
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in an embedded environment with multiple processors. I created a policy for classes, which, I had hoped, would automatically register the class with the appropriate factory: // In some header file... #include <cassert>
7
3465
by: mathieu | last post by:
Hello, I did read the FAQ on template(*), since I could not find an answer to my current issue I am posting here. I have tried to summarize my issue in the following code (**). Basically I am trying to hide the `complexity` of template from the user interface. If you look at the code DataSet should be the object that my user manipulate. Unfortunately by doing so the object returned by DataSet::Get is a FloatingPt, so without the virtual...
15
6522
by: rizwanahmed24 | last post by:
Hello i have made a custom control. i have placed a panel on it. I want this panel to behave just like the normal panel. The problem i was having is that the panel on my custom control doesnt accept other controls. The control i drag drop on it becomes the child of my custom control's parent form and not the child of my custom control. Then i added this line "" before my custom control class (i dont know what this line does). Now
3
3085
by: djsuson | last post by:
I'm trying to set up an inheritance tree that also uses templates. This is an attempt to simplify some previous code that was filled with redundancies. I'm working with g++ 3.4.6. The code is split over four files: two headers and two implamentation files, but uses the include trick mentioned on another thread to connect the header and implamentation files together. The base class header is #ifndef _BASEDATA_H_ #define _BASEDATA_H_ ...
3
2346
by: Markus Dehmann | last post by:
I have an abstract base class which contains a function that I would like to template, but virtual template functions are illegal. I put a mini code example below, which doesn't do anything great, but reflects the structure of my problem. In a nutshell, the abstr. base class VectorCreator has a function createVector whose return type is unfortunately fixed to vector<double>. Only now I discovered that some implementations would like to...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10087
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8971
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...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
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...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4046
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
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.