簡體   English   中英

((Thermostat *)this)-> Thermostat :: _ dht'沒有類類型

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

我正在嘗試創建一個名為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};
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM