C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Simple Template Question

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
LaBird
Guest





PostPosted: Wed Jul 30, 2003 11:10 am    Post subject: Simple Template Question Reply with quote



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).


Back to top
Josephine Schafer
Guest





PostPosted: Wed Jul 30, 2003 11:26 am    Post subject: Re: Simple Template Question Reply with quote




"LaBird" <kellily (AT) excite (DOT) com> wrote

Quote:
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 void C // some meaningful stuff, setting p = t
}

template <typename T
void C // other meaningful stuff, printing out p
}

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



Back to top
Peter van Merkerk
Guest





PostPosted: Wed Jul 30, 2003 11:28 am    Post subject: Re: Simple Template Question Reply with quote



"LaBird" <kellily (AT) excite (DOT) com> :
Quote:
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 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






Back to top
Agent Mulder
Guest





PostPosted: Wed Jul 30, 2003 1:39 pm    Post subject: Re: Simple Template Question Reply with quote

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
//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



Back to top
LaBird
Guest





PostPosted: Wed Jul 30, 2003 3:30 pm    Post subject: Re: Simple Template Question Reply with quote

Josephine Schafer <jsf (AT) usa (DOT) net> wrote

Quote:

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).



Back to top
LaBird
Guest





PostPosted: Wed Jul 30, 2003 3:32 pm    Post subject: Re: Simple Template Question Reply with quote

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 <merkerk (AT) deadspam (DOT) com> wrote

Quote:
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.




Back to top
Harald Grossauer
Guest





PostPosted: Wed Jul 30, 2003 3:49 pm    Post subject: Re: Simple Template Question Reply with quote

Josephine Schafer wrote:

Quote:

"LaBird" <kellily (AT) excite (DOT) com> wrote in message
news:bg8938$hib$1 (AT) www (DOT) 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 void C // some meaningful stuff, setting p = t
}

template <typename T
void C // 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...?


Back to top
Josephine Schafer
Guest





PostPosted: Thu Jul 31, 2003 3:30 am    Post subject: Re: Simple Template Question Reply with quote


"LaBird" <kellily (AT) excite (DOT) com> wrote

Quote:
Josephine Schafer <jsf (AT) usa (DOT) net> wrote in message
news:bg8a1b$ldnki$1 (AT) ID-192448 (DOT) 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.





Back to top
Agent Mulder
Guest





PostPosted: Thu Jul 31, 2003 12:35 pm    Post subject: Re: Simple Template Question Reply with quote

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


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.