0

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)'

Nujufas
  • 676
  • 1
  • 9
  • 19

0 Answers0