((恒温器*)此)->恒温器::_dht"没有类类型

((Thermostat*)this)->Thermostat::_dht' does not have class type

本文关键字:恒温器 类型 dht gt      更新时间:2023-10-16

我正在尝试创建一个名为 Thermostat 的 Arduino 类,它使用 DHT 库。

我认为错误可能是我在声明_dht实例和初始化它方面感到困惑。

我的目标只是让我的主要草图干净,让全班Thermostat处理与 DHT 相关的所有事情。

这是我的草图:

#include "Thermostat.h"

void setup() {
  // put your setup code here, to run once:
}
void loop() {
  // put your main code here, to run repeatedly:
}

这是我Thermostat.h文件:

/*
  Thermostat.h - Library for smart thermostat
*/
#ifndef Thermostat_h
#define Thermostat_h
#include "Arduino.h"
#include <DHT.h>
class Thermostat {
  public:
    Thermostat();
    void DHTstart();
  private:
    DHT _dht(uint8_t, uint8_t); //// Initialize DHT sensor for normal 16mhz Arduino
};
// class initialization 
Thermostat::Thermostat(){
  _dht(7,DHT22);
}
void Thermostat::DHTstart(){
  _dht.begin();
}

#endif

我收到以下错误:

In file included from /Users/olmo/Documents/Arduino/debug_DTH_inClass/debug_DTH_inClass.ino:2:0:
sketch/Thermostat.h: In member function 'void Thermostat::DHTstart()':
Thermostat.h:24: error: '((Thermostat*)this)->Thermostat::_dht' does not have class type
   _dht.begin();
   ^
exit status 1
'((Thermostat*)this)->Thermostat::_dht' does not have class type

这几乎是正确的,但DHT _dht(uint8_t, uint8_t);是方法原型(而不是DHT实例(。并且您必须在构造函数初始值设定项列表中初始化此实例:

class Thermostat {
  public:
    Thermostat();
    void DHTstart();
  private:
    DHT _dht; //// Initialize DHT sensor for normal 16mhz Arduino
};
// class initialization 
Thermostat::Thermostat()
: _dht(7,DHT22)  // construct DHT instance with expected parameters
{ ; }
void Thermostat::DHTstart(){
  _dht.begin();
}

或更短的版本:

class Thermostat {
  public:
    Thermostat() : _dht(7, DHT22) {;}
    void DHTstart() { _dht.begin(); }
  private:
    DHT _dht;
};

在这种情况下(DHT 类的魔术值(,您可以使用 c++11 功能(从 Arduino 1.6.5 开始(并直接指定它,因此可以使用默认构造函数:

class Thermostat {
  public:
    void DHTstart() { _dht.begin(); }
  private:
    DHT _dht{7, DHT22};
};