1

I'm having trouble with inherited methods in a class definitions in the Arduino environment. I have a base class portal which is inherited from by class gauge and then meter inherits from gauge. The base class has a definition for a method, but the compiler says it cant find a definition of meter::method. Header file:

#ifndef UIelements_h
#define UIelements_h

#include "Arduino.h"
#include "UTFT.h"
#include "URTouch.h"
#include "UTFT_Geometry.h"
    class portal
{
  public:
    UTFT* myDisplay;
    int origin[2]={0,0};
    int portalSize[2]={10,10};
    int BGColor=VGA_BLACK;
    int borderSize=1;
    int borderColour=VGA_WHITE;

    portal();
    portal(UTFT* myDisplay);
    void draw(void);
    void drawContents(void);

    private:
    bool firstdraw=true;

};

class guage :public portal
{
  public:   
    UTFT_Geometry* myGeometry;
    float scaleMin=0.0;
    float scaleMax=100.0;
    float tick=20;
    bool logScale=false;
    int scaleColour=VGA_WHITE;
    int foreColour=VGA_YELLOW;
    float redBegin=80.0;
    int redColour=VGA_RED;
    float value=0;
    float lastValue=0;

    guage();
    guage(UTFT*,UTFT_Geometry*);
    void setNewValue(float);
};

class meter :public guage
{
  public:
    float startAngle=-40.0;
    float endAngle=40.0;
    float scaleRadius=80.0;

    meter();
    meter(UTFT*,UTFT_Geometry*);
    void setValueAndDraw(float);

  private:
    void PointerDraw(float);

};

.cpp

#include "Arduino.h"
#include "UIelements.h"

portal::portal()
{
}
    portal::portal(UTFT* UTFT)
{
    // constructor: save the passed in method pointers for use in the class
    myDisplay=UTFT;
}

void portal::draw(void)
{
    // draw the contents
    if (firstdraw)
    {
    // draw background and border
    }
    else
    {
    drawContents();
    }
}

void portal::drawContents(void)
{
    //portal class has no contents to draw, but subclasses will have..
}

...

meter::meter(UTFT* UTFT,UTFT_Geometry* Geometry)
{
    // constructor save the passed in method pointers for use in the class
    myDisplay=UTFT;
    myGeometry=Geometry;
}

void meter::setValueAndDraw(float newValue)
{
    setNewValue(newValue);
    draw();
}

void meter::drawContents(void)
{
    float xcentre=origin[1]+portalsize[1]/2;
    float ycentre=origin[2]+portalSize[2]-1;
    if (firstdraw=true)
//...more code in here..
}

error message

error: no 'void meter::drawContents()' member function declared in class 'meter'

I've asked a couple of people but everyone seemed to think that the class inheritance looked OK - is this an Arduino thing, or is there something fundamental that I don't understand? Any help gratefully received. I fear that its some silly typo or omission of ; etc.

2
  • declaration of void drawContents(void) is part of portal, not meter... Commented May 5, 2017 at 11:53
  • 2
    The error message is quite clear, there is no drawContents member declared in meter class even though it does inherit drawContents member from portal base class. Commented May 5, 2017 at 11:55

1 Answer 1

1

in C++ when you want to override behavior in subclasses you must mark the function as virtual and declare them in the subclass again.

So you must do this:

class portal
{
  public: virtual void drawContents();
};

void portal::drawContents()
{
//do stuff
}

class meter : public portal
{
public:
    virtual void drawContents() override; // virtual may be omitted
};

void meter::drawContents()
{
// override behavior
}

The keyword override after drawContents is a relatively new C++ feature. It might not be implemented by the arduino compiler. You can just omit it then.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the explanation. I guess I expected the compiler to work it out for itself. Seems rather clumsy to have to define it all over again, makes the override a bit pointless in my example.
Once again thanks for the coming to my rescue so quickly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.