Short version: This is a lighting control project. Some of the clases are Pin and Channel. Channel contains a Pin. Pin is a base class for DigitalOutPin and will be the base class for AnalogOutPin, and I want Channel to be able to use any child of Pin.
Pin has an abstract method setPinValue(), but when I create a DigitalOutPin object and call setPinValue(), Pin.setPinValue() gets called, not DigitalOutPin.setPinValue() and my program doesn't work because Pin.setPinValue() is empty as it's supposed to be overwritten.
Long version: If I leave out the definition of Pin.setPinValue(), I get that most idiotic of error gcc messages, "undefined reference to `vtable for Pin'". If I make it pure virtual I get several errors in the form of
In file included from /devel/arduino/lightbright/LightBrightMain/LightBrightMain.ino:9:0:
/devel/arduino/libraries/LightBrightLib/Channel.h:21:20: error: cannot declare parameter 'pin' to be of abstract type 'Pin'
void begin(Pin pin, int channelNumber);
^
In file included from /devel/arduino/libraries/LightBrightLib/DigitalOutPin.h:10:0,
from /devel/arduino/lightbright/LightBrightMain/LightBrightMain.ino:8:
/devel/arduino/libraries/LightBrightLib/Pin.h:16:7: note: because the following virtual functions are pure within 'Pin':
class Pin
^
/devel/arduino/libraries/LightBrightLib/Pin.h:33:18: note: virtual void Pin::setPinValue(int)
virtual void setPinValue(int value)=0;
^
In file included from /devel/arduino/lightbright/LightBrightMain/LightBrightMain.ino:9:0:
/devel/arduino/libraries/LightBrightLib/Channel.h:23:9: error: invalid abstract return type for member function 'Pin Channel::getPin()'
Pin getPin();
^
In file included from /devel/arduino/libraries/LightBrightLib/DigitalOutPin.h:10:0,
from /devel/arduino/lightbright/LightBrightMain/LightBrightMain.ino:8:
/devel/arduino/libraries/LightBrightLib/Pin.h:16:7: note: since type 'Pin' has pure virtual functions
class Pin
^
Full code is at GitHub but here are the relevant parts: Pin is declared like this right now as pure virtual, but I've tried with and without =0
class Pin
{
public:
...
protected:
...
// This method is child-specific. It's empty in this base class
virtual void setPinValue(int value)=0;
};
DigitalOutPin is declared as
class DigitalOutPin : public Pin
{
public:
void on();
void off();
protected:
void setPinValue(int value);
};
and defined as
void DigitalOutPin::setPinValue(int value) {
....
}
Disclaimer: I'm primarily a Java developer so C++ OO is very different for me, but as far as I can tell this should work as designed.
Thanks for any advice you can offer.
protected- it should bepublicto match the base class definition and allow access outside the class.