473,226 Members | 1,518 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,226 software developers and data experts.

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 1574
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********@yahoo.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
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...
0
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...
2
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...
10
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
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...
7
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...
15
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...
3
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...
3
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,...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...

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.