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>:rint(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).
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>:rint(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).