C++ 基类按模板,如何编写构造函数?
如何为基类名称来自模板的类编写构造函数? 当我试图编译下面的代码时,我得到了这个错误 对IMU::IMU(int)的未定义引用 在函数“全局子对象”中C++ 基类按模板,如何编写构造函数?,c++,templates,inheritance,constructor,C++,Templates,Inheritance,Constructor,如何为基类名称来自模板的类编写构造函数? 当我试图编译下面的代码时,我得到了这个错误 对IMU::IMU(int)的未定义引用 在函数“全局子对象”中 >>main.cpp>IMU.h>IMU.cpp>main.cpp>IMU.h>IMU.cppIMU.cpp对于模板来说很奇怪,请看它应该是“否”,模板不是这样工作的。 >>>main.cpp<<< IMU<ADXL335> obj(8); >>>IMU.h<<<
>>main.cpp>IMU.h>IMU.cpp>main.cpp>IMU.h>IMU.cppIMU.cpp对于模板来说很奇怪,请看它应该是“否”,模板不是这样工作的。
>>>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)
{
}
>>>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)
{
}