为什么我的派生类构造函数不起作用?

时间:2015-03-23 00:12:30

标签: c++ inheritance arduino

我一直在努力创建一个Arduino库来控制触摸屏。我的库构建了预先存在的库,这些库被编写为与显示器和放大器接口。触摸控制器。

这是我一直在研究的构造函数:

Display.cpp

Display::Display(int displayCSPin, int displayDCPin, int touchCSPin,
                 int newBacklightPin, int newRotation, int newBrightness)
    : Adafruit_HX8357(displayCSPin, displayDCPin, -1), 
      Adafruit_STMPE610(touchCSPin)
{
//Initialize display
}

Display.h

#ifndef DISPLAY_H_
#define DISPLAY_H_

#include "arduino.h"
#include "Adafruit_GFX.h"
#include "Adafruit_HX8357.h"
#include "Adafruit_STMPE610.h"

class Display : public Adafruit_HX8357, Adafruit_STMPE610 {
public:
    Display(int displayCSPin, int displayDCPin, int touchCSPin,
            int newBacklightPin, int newRotation, int newBrightness);
};

每当我尝试编译时,编译器会忽略基类构造函数中的变量,并尝试调用没有变量的默认构造函数:

error: no matching function for call to 'Adafruit_HX8357::Adafruit_HX8357()'

我尽力解决这个问题,但没有取得任何成功。 非常感谢任何和所有的帮助!

以下是原始代码:

Display.cpp

#include "Display.h"

Display::Display(int displayCSPin, int displayDCPin, int touchCSPin, int newBacklightPin, int newRotation, int newBrightness) : Adafruit_HX8357(displayCSPin, displayDCPin, -1), Adafruit_STMPE610(touchCSPin)
{

    // tft = Adafruit_HX8357(displayCSPin, displayDCPin, -1);
    //ts = Adafruit_STMPE610(touchCSPin);
    tft.begin(HX8357D);
    ts.begin();
    tft.setRotation(newRotation);
    backlightPin = newBacklightPin;
    pinMode(backlightPin, OUTPUT);

    rotation = newRotation;
    backgroundColor = HX8357_BLACK;
    brightness = newBrightness;

}

Display.h

#ifndef DISPLAY_H_
#define DISPLAY_H_

#include "arduino.h"
#include "Adafruit_GFX.h"
#include "Adafruit_HX8357.h"
#include "Adafruit_STMPE610.h"

class Display : public Adafruit_HX8357, public Adafruit_STMPE610 {
public:
    Display(int displayCSPin, int displayDCPin, int touchCSPin, int newBacklightPin, int newRotation, int newBrightness);

    void wake();
    void sleep();
    bool isAwake();
    void setBackGroundColor(int newColor);
    int getBackgroundColor();
    void setBrightness(int newBrightness);
    int getBrightness();
    void setRotation(int newRotation);
    int getRotation();
    bool isTouched();
    Adafruit_HX8357 tft;
    Adafruit_STMPE610 ts;
    int backgroundColor;

private:
    int brightness;
    int rotation;
    int backlightPin;
    bool awake;
    bool touched;
    TS_Point p;

};

1 个答案:

答案 0 :(得分:0)

查看Adafruit_HX8357Adafruit_STMPE610可用的实际构造函数:

Adafruit_HX8357(int8_t _CS, int8_t _DC, int8_t _MOSI, int8_t _SCLK, 
                int8_t _RST, int8_t _MISO); 
Adafruit_HX8357(int8_t _CS, int8_t _DC, int8_t _RST = -1); 

Adafruit_STMPE610(uint8_t cspin, uint8_t mosipin, uint8_t misopin, uint8_t clkpin); 
Adafruit_STMPE610(uint8_t cs); 
Adafruit_STMPE610(void); 

您的Display构造函数正在尝试调用每个类的第二个构造函数。但是,Display正在传递int值,其中int8_tuint8_t值是预期的。听起来好像你的编译器没有执行从int(u)int8_t的隐式转换,所以尝试使用显式转换来强制它:

Display::Display(int displayCSPin, int displayDCPin, int touchCSPin, int newBacklightPin, int newRotation, int newBrightness)
    : Adafruit_HX8357((int8_t)displayCSPin, (int8_t)displayDCPin, -1),
      Adafruit_STMPE610((uint8_t)touchCSPin)
{
    //Initialize display
}

否则,请将Display构造函数参数更改为使用int8_tuint8_t而不是int

Display::Display(int8_t displayCSPin, int8_t displayDCPin, uint8_t touchCSPin, int newBacklightPin, int newRotation, int newBrightness)
    : Adafruit_HX8357(displayCSPin, displayDCPin, -1),
      Adafruit_STMPE610(touchCSPin)
{
    //Initialize display
}