0

how can I define member functions of a class template in a cpp file, not the class declaration header file?

I've tried:

//class.h file
template <class T>
class A{
public:
    A();
    T get_data();
private:
    T data;
};

//cpp file
template <class T>
A<T>::A(){}

template <class T>
T A<T>::get_data() { return data; }

but I get undefined refrence to... compilation-time error.

Is there any way to do this?

Mohammad
  • 11
  • 3
  • By using explicit instantiation. It's mentioned in [Why can templates only be implemented in the header file?](https://stackoverflow.com/q/495021/2752075), search for `template class` in there. – HolyBlackCat Oct 16 '21 at 21:44
  • It looks your function declaration is different from its definition - in the .h file it return "T" but in the .cpp file it returns "void" – Nikolay Oct 16 '21 at 21:48

0 Answers0