Hi,
I am having trouble getting to grips with programming using templates in C++.
Consider the following files.
C.h
#ifndef _C_H
#define _C_H
template <class T>
class C {
public:
C();
virtual ~C();
}
#endif _C_H
C.cpp
#include "C.h"
template <class T>
C<T>::C() {
}
template <class T>
C<T>::~C() {
}
I try instantiate an instance of C in a file called main.cpp.
#include "C.h"
int main(int argc, char** argv) {
C<int> c;
}
I get the following error.
main.cpp undefined reference to `C<int>::C()'
I then run
g++ -o C.o C.pp
g++ -o main.o main.cpp
but get the error
main.cpp: undefined reference to `C<int>::C()'
main.cpp: undefined reference to `C<int>::~C()'
I am sure this probably an obvious mistake, but I am a real beginner at this so would appreciate any help.
Thanks!