I have 2 files I am working on: encoder.h and encoder.cc.
As far as I know the inherited methods (initialize and read) should be available in the MotorEncoder class. However when I try to implement the methods the compiler throws an error. Any ideas?
Header
class Encoder { protected: u32 bitResolution; SPI spi; public: void initialize(u16 spiDeviceID, u32 bitResolution); u32 read(); }; class MotorEncoder : public Encoder { public: // If I comment these 2 lines it SHOULD work as far as I know, // but it won't compile due to the method not being defined when // implementing e.g. in MotorEncoder::initialize() void initialize(u16 spiDeviceID, u32 bitResolution); u32 read(); }; Implementation
void Encoder::initialize(u16 spiDeviceID, u32 bitResolution) { // .... } u32 Encoder::read() { // } void MotorEncoder::initialize(u16 spiDeviceID, u32 bitResolution) { // implementation } u32 MotorEncoder::read() { // implementation code }
initializeandreadasvirtualinclass Encoderinitialize- andread- methods should bevirtualin base class.virtualkeyword. The base and derived function signature must match.override- that will turn a signature mistake in the derived class into an error. Always use it when deriving from a base class from a library.