in header testh.h
, i have the following
#include<stdlib.h>
template<typename T>
class X {
public:
T x;
X(T i);
void assign(T *i);
};
the file obj.cpp
, has the following
#include<testh.h>
template<typename T>
X<T>::X(T i) {
//...
}
(using obj.cpp
as object files(.o
) for linking)
in the main call in file test.cpp
, has the following
#include<testh.h>
int main() {
X<int> x(20);
return 0;
}
but calling the constructor of X
with template[T=int], get the linker error
/usr/bin/ld: /tmp/ccHiPTCC.o: in function `main':
test.cpp:(.text+0x28): undefined reference to `X<int>::X(int)'
collect2: error: ld returned 1 exit status
is there a get around for defining class members in a separate file while using templates?