0

How to write constructor for class that base class name is from template? as I try to compile code below then i get this error

undefined reference to >IMU<ADXL>::IMU(int)

In function `_GLOBAL__sub_I_rdy'

>>>main.cpp<<<
IMU<ADXL335> obj(8);

>>>IMU.h<<<
template <class T>
class IMU : public T
{
   public:
   const int someMember=0; 
   IMU(int);
};

>>>IMU.cpp<<<
template <class T>
IMU<T>::IMU(int x) // IMU constructor
: T()         // base constructor
, someMember(x)
{

}

it should work as

>>>main.cpp<<<
IMU obj(8);

>>>IMU.h<<<
class IMU : public ADXL
{
   public:
   const int someMember=0;       
   IMU(int);
};

>>>IMU.cpp<<<
IMU::IMU(int x) // IMU constructor
: ADXL()   // ADXL constructor
, someMember(x)
{

}
Dobijasek
  • 91
  • 1
  • 5
  • IMU.cpp for template is strange, see [why-can-templates-only-be-implemented-in-the-header-file](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file?r=SearchResults&s=1|584.5269) – Jarod42 May 12 '21 at 15:24
  • *it should work as* No, that's not how templates work. – Eljay May 12 '21 at 16:19

0 Answers0