((恒温器*)此)->恒温器::_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};
};
相关文章:
- 如何在c++迭代器类型中包装std::chrono
- 参数包中的筛选器类型
- 定义模板参数的迭代器类型
- C++如何乘以包含 std::variant 元素的向量的迭代器?正在执行迭代器类型的转换?
- C++不兼容的迭代器类型
- 如何推导 std::高级迭代器类型?
- 在 stl 容器包装器中定义迭代器类型
- Spirit.X3 与 lambda 返回不同的解析器类型
- Objective-C C++ 包装器 类型错误的不完整定义
- 矢量迭代器类型未知
- 表达式到C++类类型转换器/类型转换
- 咖啡"未知求解器类型:SGD"
- 有什么东西可以一般地反转C++比较器类型
- STD ::向量迭代器类型和允许的操作
- 检查容器模板类中的迭代器类型
- ((恒温器*)此)->恒温器::_dht"没有类类型
- 如何断言模板参数的类型 STL 迭代器类型
- 检测容器是否具有迭代器类型
- 由set2.begin()返回的迭代器类型
- 序列容器的迭代器类型是什么?