By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,188 Members | 980 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,188 IT Pros & Developers. It's quick & easy.

Simple Template Question

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a

"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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.