无法实例化我自己的 class:没有匹配函数来调用 'DHT::DHT()'
Cannot instantiate my own class: no matching function for call to 'DHT::DHT()'
我正在编写一个 Arduino 项目并尝试将我自己的 class 包裹在 DHT(传感器)库(https://github.com/adafruit/DHT-sensor-library/blob/master/examples/DHT_Unified_Sensor/DHT_Unified_Sensor.ino or that https://github.com/adafruit/DHT-sensor-library/blob/master/examples/DHTtester/DHTtester.ino)中。
我想用函数抽象出我自己的库,并用它来学习 C++。
但是当我尝试实例化我自己的 class 时遇到问题:
lib/Hythe/Hythe.cpp: In constructor 'Hythe::Hythe(uint8_t, uint8_t)':
lib/Hythe/Hythe.cpp:3:47: error: no matching function for call to 'DHT::DHT()'
Hythe::Hythe(uint8_t dht_pin, uint8_t dht_type)
谁能告诉我如何在我自己的 class 中声明、实例化和调用 DHT class?
当我实施 DHT
main.cpp
#include "Hythe.h"
Hythe hythe(HYTHE_PIN, HYTHE_TYPE); // here I instantiate the sensor.
void setup()
{
hythe.getSensorInfo();
}
Hythe.h
#include "SPI.h"
#include "Wire.h"
#include <DHT.h>
class Hythe
{
private:
float temperature;
float humidity;
uint32_t delayMS;
public:
Hythe(uint8_t, uint8_t); // init with PIN and TYPE
DHT _dht; // THIS IS CAUSING THE ERROR
// (It's in the header file. I simply wanna declare it and
// call it in the cpp file later).
// When I remove this line I get:
// "error: '_dht' was not declared in this scope"
unsigned char getTemperature(); // read the temperature
void getSensorInfo(); // returns sensor info
};
Hythe.cpp
#include "Hythe.h"
Hythe::Hythe(uint8_t dht_pin, uint8_t dht_type)
{
Serial.print(dht_pin);
_dht = DHT(dht_pin, dht_type);
}
unsigned char Hythe::getTemperature()
{
return 0;
}
void Hythe::getSensorInfo()
{
Serial.println(F("------------------------------------"));
Serial.println(F("Temperature Sensor"));
}
正如 Vlad 指出的那样,DHT
没有默认构造函数。你想使用 initializer list 来避免调用默认构造函数。这样就可以在初始化列表中直接调用DHT的构造函数了
Hythe::Hythe(uint8_t dht_pin, uint8_t dht_type): _dht(dht_pin, dht_type)
{
Serial.print(dht_pin);
}
我正在编写一个 Arduino 项目并尝试将我自己的 class 包裹在 DHT(传感器)库(https://github.com/adafruit/DHT-sensor-library/blob/master/examples/DHT_Unified_Sensor/DHT_Unified_Sensor.ino or that https://github.com/adafruit/DHT-sensor-library/blob/master/examples/DHTtester/DHTtester.ino)中。
我想用函数抽象出我自己的库,并用它来学习 C++。
但是当我尝试实例化我自己的 class 时遇到问题:
lib/Hythe/Hythe.cpp: In constructor 'Hythe::Hythe(uint8_t, uint8_t)':
lib/Hythe/Hythe.cpp:3:47: error: no matching function for call to 'DHT::DHT()'
Hythe::Hythe(uint8_t dht_pin, uint8_t dht_type)
谁能告诉我如何在我自己的 class 中声明、实例化和调用 DHT class? 当我实施 DHT
main.cpp
#include "Hythe.h"
Hythe hythe(HYTHE_PIN, HYTHE_TYPE); // here I instantiate the sensor.
void setup()
{
hythe.getSensorInfo();
}
Hythe.h
#include "SPI.h"
#include "Wire.h"
#include <DHT.h>
class Hythe
{
private:
float temperature;
float humidity;
uint32_t delayMS;
public:
Hythe(uint8_t, uint8_t); // init with PIN and TYPE
DHT _dht; // THIS IS CAUSING THE ERROR
// (It's in the header file. I simply wanna declare it and
// call it in the cpp file later).
// When I remove this line I get:
// "error: '_dht' was not declared in this scope"
unsigned char getTemperature(); // read the temperature
void getSensorInfo(); // returns sensor info
};
Hythe.cpp
#include "Hythe.h"
Hythe::Hythe(uint8_t dht_pin, uint8_t dht_type)
{
Serial.print(dht_pin);
_dht = DHT(dht_pin, dht_type);
}
unsigned char Hythe::getTemperature()
{
return 0;
}
void Hythe::getSensorInfo()
{
Serial.println(F("------------------------------------"));
Serial.println(F("Temperature Sensor"));
}
正如 Vlad 指出的那样,DHT
没有默认构造函数。你想使用 initializer list 来避免调用默认构造函数。这样就可以在初始化列表中直接调用DHT的构造函数了
Hythe::Hythe(uint8_t dht_pin, uint8_t dht_type): _dht(dht_pin, dht_type)
{
Serial.print(dht_pin);
}