我在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的東西,還是有一些我不明白的基礎知識?任何幫助感激地收到。我擔心它的一些愚蠢的錯誤或遺漏;
等
'void drawContents(void)'的聲明是'portal'的一部分,而不是'meter' ... – Jarod42
錯誤信息非常清晰,即使它繼承了,也沒有在meter類中聲明的'drawContents'成員來自'portal'基類的'drawContents'成員。 – VTT