I have the following code:
test.h
struct A {void funA() { cout<<"Hello"; } };
template<typename T>
struct B: public T {
template<typename X>
void funB(X x) {
this->funA();
cout<<" world, I'm " <<typeid(x).name()<<"\n";
}
};
template class B<A>;
main.cpp
int main() {
B<A> o;
o.funB('a');
o.funB(1);
}
Yields:
Hello world, I'm c
Hello world, I'm i
No Problem!
But I'm unable to move the definition of the function 'funB' to test.cpp.
I tried the following and failed:
test.h
...
template<typename X>
void funB(X x);
...
test.cpp
...
template<typename T>
template<typename X>
void B<T>::funB(X x) {
this->funA();
cout<<" world, I'm " <<typeid(x).name()<<"\n";
}
...
Yields:
undefined reference to 'void B::funB(char)'
undefined reference to 'void B::funB(int)'
which becomes a little verbose if I start having a lot of types of T and U* part. The last bit is the shortcut the OP tried since it can get very verbose.
– NathanOliver Dec 04 '19 at 16:19