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

Using templates

Hi,

I'm trying to write template functions and use them.

Is it possible to declare a template function in a .h file and define
it in a .cpp file.

Ex:

//temp_func.h
template <class T>void swap(T &left,T &right);

//temp_func.cpp
template <class T>void swap(T &left,T &right)
{
T temp = left;
left = right;
right = temp;
}

//main.cpp
#include <iostream>
#include "temp_func.h"

using namespace std;
main()
{
int a = 10, b = 20;
cout<<"Before swapping: a = "<<a<<", b = "<<b<<endl;
swap(a,b);
cout<<"After swapping: a = "<<a<<", b = "<<b<<endl;
}

When I built the above code, I got the following error:

undefined reference to 'void swap<int>(int&,int&)'

I know that the instantiation of the swap() function is not available
in temp_func.o.

But, how can one make the above code build successfully.

The above code builds properly if I define the swap() function in
the .h file itself.

How do the STL algorithms work?

Are those functions defined in the headers itself.

Thanks in advance.

Regards
Sunil
Jan 15 '08 #1
2 1501
On 15 Jan, 11:43, Sunil Varma <sunil....@gmail.comwrote:
Hi,

I'm trying to write template functions and use them.
[snip]

See http://www.parashift.com/c++-faq-lit...html#faq-35.12

hth

Jan 15 '08 #2
On Jan 15, 12:43 pm, Sunil Varma <sunil....@gmail.comwrote:
I'm trying to write template functions and use them.
Is it possible to declare a template function in a .h file and
define it in a .cpp file.
Yes, but...
Ex:
//temp_func.h
template <class T>void swap(T &left,T &right);
You need to declare it extern here. Which is a problem, since a
lot of compilers aren't very up-to-date, and don't support
extern.
//temp_func.cpp
template <class T>void swap(T &left,T &right)
{
T temp = left;
left = right;
right = temp;
}
This is, obviously, the only reasonable organization. Given
that compilers don't support extern, howver, what you have to do
is add an ``#include "temp_func.cpp"'' to the end of your .h
file (and of course, ensure that the .cpp is exported along with
the .h file, when you provide a library).

A common convention is to use still a different naming
convention for these files. G++, for example, terminates them
with .tcc (as opposed to .cc and .hh). Typically, they are
designed to work only when included at the end of the .hh, and
shouldn't be included anywhere else.

Another possibility would be to use export when available, using
conditional compilation. Basically, your header looks something
like:

#ifndef TEMP_FUNC_CPP_andSomeRandomJunk

export template< typename T void swap( T& left, T& right ) ;

#ifdef export
#include "temp_func.cpp"
#endif
#endif

If the compiler doesn't support export, you invoke it with
something like -Dexport= or /Dexport=, whatever is necessary to
define "export" as an empty string.

[...]
How do the STL algorithms work?
Are those functions defined in the headers itself.
It depends on the implementation---g++ does as I described, it
puts the implementations in a .tcc file, which is included from
the .hh.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 16 '08 #3

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

Similar topics

2
by: Sam | last post by:
I would like to store html templates in a database. By using perl I would like to retrive the template ask the user to fill the template and store the whole file is template + the user data in a...
2
by: nanookfan | last post by:
Hi all, I'm having a bizarre problem converting XML files to HTML using an XSLT. The problem is only occuring in my Netscape 7.0 browser. What makes it more bizarre is that it is only...
3
by: Sandros | last post by:
Background: I'm collecting usability statistics for a group of applications. Each count has the following attributes: date, application, major heading, minor heading, count. My intent is to pull...
4
by: Pat Turner | last post by:
Hi, I have some XML like this: <family> <person name="bob"> <father ref="../../person" /> </person> <person name="charlie"> <child ref="../../person" />
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
6
by: Mark Miller | last post by:
I have a scheduled job that uses different XSL templates to transform XML and save it to disk. I am having problems with the code below. The problem shows up on both my development machine (Windows...
0
by: Piper707 | last post by:
I need help with using a general template which would process all tags other than the ones for which specific templates have been written. My XML looks like this: <ITEMS>...
1
by: oldgent | last post by:
I am having a problem installing the starter kits. I have reinstalled VS 2005, think that might be the problem. I then installed both 'Personal Website" and the "Club Website" starter kits. I...
28
by: NewToCPP | last post by:
Hi, I am just trying to find out if there is any strong reason for not using Templates. When we use Templates it is going to replicate the code for different data types, thus increasing the...
6
by: John Larson | last post by:
Hi All, I am some information from INSPEC database records in XML to build a relational database of my own. I am currently trying to extract information by doing an XSLT transform of the XML...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.