Simple Template Question

L

LaBird

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

Josephine Schafer

LaBird said:
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.
 
P

Peter van Merkerk

LaBird said:
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.
 
A

Agent Mulder

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;
}
 
L

LaBird

Josephine Schafer said:
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).
 
L

LaBird

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

Harald Grossauer

Josephine said:
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...?
 
J

Josephine Schafer

LaBird said:
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.
 
A

Agent Mulder

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top