2017-05-05 17 views
1

我在Arduino環境中的類定義中遇到了繼承方法的問題。我有一個基類portal,它繼承自類gauge,然後meter繼承自gauge。基類有一個方法的定義,但是編譯器說它找不到meter::method的定義。 頭文件:arduino C++中的繼承問題

#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: no 'void meter::drawContents()' member function declared in class 'meter'

我問了幾個人,但每個人似乎認爲類繼承看起來不錯 - 是這是一個Arduino的東西,還是有一些我不明白的基礎知識?任何幫助感激地收到。我擔心它的一些愚蠢的錯誤或遺漏;

+0

'void drawContents(void)'的聲明是'portal'的一部分,而不是'meter' ... – Jarod42

+2

錯誤信息非常清晰,即使它繼承了,也沒有在meter類中聲明的'drawContents'成員來自'portal'基類的'drawContents'成員。 – VTT

回答

1

在C++中,當你想覆蓋子類中的行爲時,你必須將該函數標記爲虛函數並再次在子類中聲明它們。

所以,你必須這樣做:

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 
} 

關鍵字覆蓋drawContents後是一個相對較新的C++功能。它可能不是由arduino編譯器實現的。那麼你可以忽略它。

+0

感謝您的解釋。我想我希望編譯器自己解決它。似乎很難再次定義它,在我的例子中使覆蓋有點沒有意義。 – BrightSparkey

+0

再次感謝即將來拯救我。 – BrightSparkey