[英]((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.