473,394 Members | 1,718 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.

Simple Template Question

Hi,

I have a simple question about templates.

When I have a class definition like this in "class.h":

template <typename T>
class C {
T p;
public:
void set(T);
void print(T);
};

And then I implement the member functions in "class.cpp":

#include "class.h"

template <typename T>
void C<T>::set(T t) {
// some meaningful stuff, setting p = t
}

template <typename T>
void C<T>::print(T t) {
// other meaningful stuff, printing out p
}

Now I would like to use the class in main() in "main.cpp".
How should I call it? I tried the following:

#include "class.h"

int main() {
C<int> object;
object.set(7);
// ...
return 0;
}

But when I compile with "g++ class.cpp main.cpp",
I get "undefined reference" for the C<T>::set(T) function
in main.cpp. How to overcome this, apart from merging
class.cpp and main.cpp together?

Thanks in advance.

Regards,
LaBird (Benny).
Jul 19 '05 #1
8 2572

"LaBird" <ke*****@excite.com> wrote in message
news:bg**********@www.csis.hku.hk...
Hi,

I have a simple question about templates.

When I have a class definition like this in "class.h":

template <typename T>
class C {
T p;
public:
void set(T);
void print(T);
};

And then I implement the member functions in "class.cpp":

#include "class.h"

template <typename T>
void C<T>::set(T t) {
// some meaningful stuff, setting p = t
}

template <typename T>
void C<T>::print(T t) {
// other meaningful stuff, printing out p
}


Copy all the functions into class.h.
Throw away class.cpp.
Jul 19 '05 #2
"LaBird" <ke*****@excite.com> :
When I have a class definition like this in "class.h":

template <typename T>
class C {
T p;
public:
void set(T);
void print(T);
};

And then I implement the member functions in "class.cpp": [snip] Now I would like to use the class in main() in "main.cpp".
How should I call it? I tried the following:

#include "class.h"

int main() {
C<int> object;
object.set(7);
// ...
return 0;
}

But when I compile with "g++ class.cpp main.cpp",
I get "undefined reference" for the C<T>::set(T) function
in main.cpp. How to overcome this, apart from merging
class.cpp and main.cpp together?


If you want to put the implementation of template function into a .cpp
file, you will need a compiler that supports the 'export' keyword, at
this moment very few compilers do. For one that supports 'export' see:
http://www.comeaucomputing.com. Without the 'export' keyword you will
have to put inline definitions of the functions into the class.h header
file.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl

Jul 19 '05 #3
LB> I would like to use the class in main() in "main.cpp".
LB> How should I call it?

Stick to this style and you'll never
have any problems with your #include's.

//This is file C.h. All headers needed go to main.cpp
template<typename T>class C
{
T p;
public:
void set(T t){p=t;}
void print(T t){cout<<"\nif it ain't got that swing";}
};

//This if file Music.h. All headers needed go to main.cpp
class Music{};

//This is file Jazz.h. All headers needed go to main.cpp
class Jazz:public C<Music>{};

//This is file main.cpp. All #include's go here
#include <iostream.h>
#include "C.h"
#include "Music.h"
#include "Jazz.h"
int main(int argv,char**argc)
{
C<int>object;
object.set(7);
Jazz jazz;
jazz.print(*new Music);
return 0;
}

-----------------------------------
output

if it ain't got that swing

Jul 19 '05 #4
Josephine Schafer <js*@usa.net> wrote in message
news:bg************@ID-192448.news.uni-berlin.de...

Copy all the functions into class.h.
Throw away class.cpp.


A follow-up question: As my actual class definiton is more complicated,
and the member functions of the class contain lots of accesses to global
variables, which are initialized at the global scope, something like:

unsigned int count = 0;
template<typename T> class C<T>::set(T t) {
if (count % 2 == 0)
p = t;
else
p = 0 - t;
count++;
}

Can I put all these code into the .h as well?

Regards,
LaBird (Benny).
Jul 19 '05 #5
Thank you very much, I did not realize before that my question is actually
not as simple as I thought to be. :)

Regards,
LaBird (Benny).
Peter van Merkerk <me*****@deadspam.com> wrote in message
news:bg************@ID-133164.news.uni-berlin.de...
If you want to put the implementation of template function into a .cpp
file, you will need a compiler that supports the 'export' keyword, at
this moment very few compilers do. For one that supports 'export' see:
http://www.comeaucomputing.com. Without the 'export' keyword you will
have to put inline definitions of the functions into the class.h header
file.


Jul 19 '05 #6
Josephine Schafer wrote:

"LaBird" <ke*****@excite.com> wrote in message
news:bg**********@www.csis.hku.hk...
Hi,

I have a simple question about templates.

When I have a class definition like this in "class.h":

template <typename T>
class C {
T p;
public:
void set(T);
void print(T);
};

And then I implement the member functions in "class.cpp":

#include "class.h"

template <typename T>
void C<T>::set(T t) {
// some meaningful stuff, setting p = t
}

template <typename T>
void C<T>::print(T t) {
// other meaningful stuff, printing out p
}


Copy all the functions into class.h.
Throw away class.cpp.


Is the STL implemented that way? Everything in the headers and no .cpp's?

Is it possible at all to write a "template library" and link dynamically
against it? I guess it would be possible since all necessary function
pointers should be available, but they would have to be determined at
runtime for every template call...?

Jul 19 '05 #7

"LaBird" <ke*****@excite.com> wrote in message
news:bg**********@www.csis.hku.hk...
Josephine Schafer <js*@usa.net> wrote in message
news:bg************@ID-192448.news.uni-berlin.de...

Copy all the functions into class.h.
Throw away class.cpp.


A follow-up question: As my actual class definiton is more complicated,
and the member functions of the class contain lots of accesses to global
variables, which are initialized at the global scope, something like:

unsigned int count = 0;
template<typename T> class C<T>::set(T t) {
if (count % 2 == 0)
p = t;
else
p = 0 - t;
count++;
}

Can I put all these code into the .h as well?


See anything preceded by a template<...> means compiler won't allocate
storage for it at that point, but will instead wait until the template
instantiation.
Also somwhere in the linker and compiler there's a mechanism to remove
multiple definitions of identical templates. So for ease of use you would
almost always put the entire template declarartion and definition in the
header file.


Jul 19 '05 #8
LB> A follow-up question: As my actual class definiton is more complicated,
LB> and the member functions of the class contain lots of accesses to global
LB> variables, which are initialized at the global scope, something like:
LB>
LB> unsigned int count = 0;
LB> template<typename T> class C<T>::set(T t) {
LB> if (count % 2 == 0)
LB> p = t;
LB> else
LB> p = 0 - t;
LB> count++;
LB> }
LB>
LB> Can I put all these code into the .h as well?

Yes, that's my style. When I work on global
instances of my objects, I create the objects at the start
of the .h file and declare the class or template that
works on the global instances right under it.
The objects are encapsulated, not at class level
but at module (file) level. Works fine.

-X
Jul 19 '05 #9

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

Similar topics

1
by: Scott | last post by:
The following is the XML I have to work with. Below is the question <Table0> <CaseID>102114</CaseID> <CaseNumber>1</CaseNumber> <DateOpened>2005-06-14T07:26:00.0000000-05:00</DateOpened>...
3
by: Winston Smith | last post by:
Hi, I have question about compiling multiple source files with templates using gcc. It's probably obvious but I'm new to C++. I've tried to isolate my problem and it came down to this. Lets...
13
by: dgront | last post by:
Maybe my question is too simple, but I've spent some time on it and still don't know.. I need a template function, converting from string to other data types: template <typename T>...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
0
by: 42 | last post by:
I implemented a simple class inherited from Page to create a page template. It simply wraps some trivial html around the inherited page, and puts the inherited page into a form. The problem I...
11
by: aaragon | last post by:
Hi everyone, I'm trying to create two simple template classes where one of them depends on the other to initialize its member integer variable. After some time, I decided to post a message here...
3
by: Chrism2671 | last post by:
I'm new to XSLT/XML and I have a very simple, quick question. i've been trying to convert simple xml files into CSV files and have made a simple XSLT template using the w3 tutorials, but it doesn't...
3
by: stdlib99 | last post by:
Hi, I have a simple question regarding templates and meta programming. I am going to try and work my way through the C++ Template Metaprogramming, a book by David Abrahams and Aleksey...
17
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /*...
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...
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
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,...
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
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...

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.