简体   繁体   中英

passing an object as a parameter to a constructor of another class C++

I'm trying to make a user interface class for display the weather forecast retrieve from an API, using a Nokia5110 LCD, but I'm getting an error when I try to pass a reference to an Adafruit_PCD8544 Object from the.ino to the constructor of the class.

Some help will be highly appreciated. The error i get:

"error: no matching function for call to Adafruit_PCD8544::Adafruit_PCD8544() "

this is the main.ino file:

// Nokia 5110 LCD pinout connections to nodeMCU8266
#define CLK_PIN D1  // Serial clock out (SCLK)
#define DIN_PIN D2  // Serial data out (DIN)
#define DC_PIN  D5  // Data/Command select (D/C)
#define CS_PIN  D6  // lCD chip select (CS)
#define RST_PIN D4  // LCD reset (RST)

Adafruit_PCD8544 display = Adafruit_PCD8544(CLK_PIN, DIN_PIN, DC_PIN, CS_PIN, RST_PIN);
UI_Nokia5110 UI(display);

this is the header file for the UI.h:

#ifndef UI_WEATHER_API_H
#define UI_WEATHER_API_H

#include <Arduino.h>

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>

class UI_Nokia5110{
  private:
    Adafruit_PCD8544 display ;
  public:
    UI_Nokia5110(){}
    UI_Nokia5110(Adafruit_PCD8544 &display);
    
};
#endif

here, the implementation UI.cpp

#include "UI_Nokia5110.h"

UI_Nokia5110::UI_Nokia5110(Adafruit_PCD8544 &display){
  this->display = display;
}

Members are initialized before the constructor body is executed. The constructor body is not the place to initialize members. If you do not provide an initializer, the member display will be default constructed. The error says Adafruit_PCD8544 has no default constructor. Use the member initializer list:

UI_Nokia5110::UI_Nokia5110(Adafruit_PCD8544 &display) : display(display) {
    // nothing to be done here, members are already initialized
}

For more details I refer you to https://en.cppreference.com/w/cpp/language/constructor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM