winlinvip / SimpleDHT

Simple, Stable and Fast Arduino Temp & Humidity Sensors for DHT11 and DHT22. http://learn.adafruit.com/dht
MIT License
145 stars 61 forks source link

Empty constructor fails, I don't understand why #53

Open DaleSchultz opened 1 year ago

DaleSchultz commented 1 year ago

SimpleDH11 defines two public constructors:

    SimpleDHT11();
    SimpleDHT11(int pin);

and they are implemented like this:

SimpleDHT11::SimpleDHT11() {
}
SimpleDHT11::SimpleDHT11(int pin) : SimpleDHT (pin) {
}

Which all looks perfectly fine. If, however, we declare an object of type SimpleDHT11 without a pin, the sketch fails to compile.

Here is a minimal sketch to show it.

#include <SimpleDHT.h>

int pinDHT11 = 14;

SimpleDHT11 dht11(); // fails, error: request for member 'read2' in 'dht11', which is of non-class type 'SimpleDHT11()'
//SimpleDHT11 dht11(-1); //works fine

void setup() {
  Serial.begin(115200);
}

void loop(){
  float temperature = 0;
  float humidity = 0;
  int err = dht11.read2( pinDHT11, &temperature, &humidity, NULL);
  Serial.println(humidity);
  delay(1200);
}

If we pass in a dummy pin number, such as -1, the code compiles and works. I pass in the pin number to the read/read2 function which are all overloaded to include, or not include, the pin.

Because of the design of my application, I wish to be able to specify the pin on each call to read, so my plan was to not specify a pin in the constructor call. I have no problem passing in a dummy value, but I am trying to understand why it does not work without.

This is happening on a Wemos D1 (ESP8266), Arduino IDE 2.0.3