我想将两个对象传递到另一个对象的构造函数中。
我创建了一个ClimateController
类。
每个ClimateController
实例应具有一个DHT
和一个Relay
对象。
DHT
对象将是每个ClimateController
对象的共享实例,因为只有一个传感器。Relay
实例,ClimateController
对象应该是 unique 实例,因为每个控制器都应具有自己的中继。我写了一个草图,但是目前不起作用。我希望有人可以帮助我。我是C ++的新手,但是有Java经验。也许我在用Java方式想很多...
这是我的草图:
#include "Relay.h"
#include "libs/DHT-sensor-library/DHT.h"
#include "libs/Wagter_ClimateController/ClimateController.h"
DHT* dht;
Relay* relay1;
Relay* relay2;
ClimateController* heatingController;
ClimateController* coolingController;
void setup() {
pinMode(PIN0, INPUT);
pinMode(PIN1, OUTPUT);
pinMode(PIN2, OUTPUT);
dht = new DHT(PIN0, DHT22, 1);
relay1 = new Relay(PIN1, false);
relay2 = new Relay(PIN2, false);
heatingController = new ClimateController(
relay1,
dht,
0.0,
30.0
);
coolingController = new ClimateController(
relay2,
dht,
32.0,
100.0
);
}
void loop() {
heatingController.poll(;)
coolingController.poll();
}
编译时,出现以下异常:
==================== [构建|无标题1 |调试] =============================== /home/joris/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/183.5429.37/bin/cmake/linux/bin/cmake --build / home / joris / CLionProjects / untitled1 / cmake-build-debug --target无标题1--j 2 [87%]构建目标uno_CORE [91%]构建CXX 对象CMakeFiles / untitled1.dir / untitled1_untitled1.ino.cpp.obj /home/joris/CLionProjects/untitled1/untitled1.ino:在功能‘void setup()”:/ home / joris / CLionProjects / untitled1 / untitled1.ino:27:5: 错误:没有匹配的调用函数 ‘ClimateController :: ClimateController(Relay *&,DHT *&,double,double)” ); ^在/home/joris/CLionProjects/untitled1/untitled1.ino:3:0中包含的文件中: /home/joris/CLionProjects/untitled1/libs/Wagter_ClimateController/ClimateController.h:22:5: 注意:候选人:ClimateController :: ClimateController(Relay,DHT, 浮动,浮动) ClimateController(继电器,DHT传感器,最小浮点值,最大浮点值); ^ /home/joris/CLionProjects/untitled1/libs/Wagter_ClimateController/ClimateController.h:22:5: 注意:参数1从'Relay *'到'Relay'的未知转换 /home/joris/CLionProjects/untitled1/libs/Wagter_ClimateController/ClimateController.h:11:7: 注意:候选人:ClimateController :: ClimateController(const ClimateController&)类ClimateController { ^ /home/joris/CLionProjects/untitled1/libs/Wagter_ClimateController/ClimateController.h:11:7: 注意:候选人期望1个论点,提供4个 /home/joris/CLionProjects/untitled1/untitled1.ino:34:5:错误:否 调用的匹配功能 ‘ClimateController :: ClimateController(Relay *&,DHT *&,double,double)” ); ^在/home/joris/CLionProjects/untitled1/untitled1.ino:3:0中包含的文件中: /home/joris/CLionProjects/untitled1/libs/Wagter_ClimateController/ClimateController.h:22:5: 注意:候选人:ClimateController :: ClimateController(Relay,DHT, 浮动,浮动) ClimateController(继电器,DHT传感器,最小浮点值,最大浮点值); ^ /home/joris/CLionProjects/untitled1/libs/Wagter_ClimateController/ClimateController.h:22:5: 注意:参数1从'Relay *'到'Relay'的未知转换 /home/joris/CLionProjects/untitled1/libs/Wagter_ClimateController/ClimateController.h:11:7: 注意:候选人:ClimateController :: ClimateController(const ClimateController&)类ClimateController { ^ /home/joris/CLionProjects/untitled1/libs/Wagter_ClimateController/ClimateController.h:11:7: 注意:候选人期望1个论点,提供4个 CMakeFiles / untitled1.dir / build.make:66:目标配方 'CMakeFiles / untitled1.dir / untitled1_untitled1.ino.cpp.obj'失败 make [3]: * [CMakeFiles / untitled1.dir / untitled1_untitled1.ino.cpp.obj]错误1 CMakeFiles / Makefile2:173:目标配方 'CMakeFiles / untitled1.dir / all'失败[2]: [CMakeFiles / untitled1.dir / all]错误2 CMakeFiles / Makefile2:185: 目标“ CMakeFiles / untitled1.dir / rule”的配方制作失败[1]: [CMakeFiles / untitled1.dir / rule]错误2 Makefile:157:配方 目标'untitled1'失败:* [untitled1]错误2
这是ClimateController.h
文件:
#ifndef _CLIMATE_CONTROLLER_h
#define _CLIMATE_CONTROLLER_h
#include "Arduino.h"
#include "Relay.h"
#include "libs/Adafruit_Sensor/Adafruit_Sensor.h"
#include "libs/DHT-sensor-library/DHT.h"
class ClimateController {
protected:
Relay relay;
DHT sensor;
float minValue;
float maxValue;
float currentValue;
bool state;
public:
ClimateController(Relay relay, DHT sensor, float minValue, float maxValue);
void poll();
float getMinValue();
float getMaxValue();
float getCurrentValue();
bool getState();
};
#endif
这是我的ClimateController.cpp
文件:
#include "ClimateController.h"
ClimateController::ClimateController(Relay relay, DHT sensor, float minValue, float maxValue) {
this->relay = relay;
this->sensor = sensor;
this->minValue = minValue;
this->maxValue = maxValue;
this->currentValue = 0;
}
float ClimateController::getCurrentValue() {
return this->currentValue;
}
float ClimateController::getMinValue() {
return this->minValue;
}
float ClimateController::getMaxValue() {
return this->maxValue;
}
bool ClimateController::getState() {
return this->state;
}
void ClimateController::poll() {
//@TODO: implement poll method
}
感谢@fonZ我修复了它。这是我的更正代码:
#include <DHT_U.h>
#include <Adafruit_Sensor.h>
#include <Relay.h>
class ClimateController {
protected:
const Relay &relay;
DHT *sensor;
double minValue;
double maxValue;
double currentValue;
bool state;
public:
ClimateController(const Relay &relay, DHT *sensor, double minValue, double maxValue)
: sensor(sensor), relay(relay) {
this->minValue = minValue,
this->maxValue = maxValue;
this->currentValue = 0;
}
double getCurrentValue() {
return currentValue;
}
double getMinValue() {
return minValue;
}
double getMaxValue() {
return maxValue;
}
bool getState() {
return state;
}
void poll() {
//@TODO: Implement poll method
}
};
ClimateController *heatingController;
ClimateController *coolingController;
void setup() {
pinMode(PIN0, INPUT);
pinMode(PIN1, OUTPUT);
pinMode(PIN2, OUTPUT);
DHT *dht = new DHT(PIN0, DHT22, 1);
heatingController = new ClimateController(Relay(PIN1, false), dht, 0.0, 30.0);
coolingController = new ClimateController(Relay(PIN2, false), dht, 32.0, 100.0);
}
void loop() {
heatingController->poll();
coolingController->poll();
}
答案 0 :(得分:1)
您的问题是您没有区分普通对象,引用和指针。在Java中,所有内容都是指针,因此不需要其他语法。在C ++中有很大的不同。
在堆上创建一个指针:(如果不在创建它的作用域的末尾删除它,它将继续存在)
DHT *dht = new DHT(PIN0, DHT22, 1); // this is a pointer
在堆栈上创建一个对象:(您可以将其视为局部变量,因为作用域一结束,对象就会被自动删除)
DHT dht = DHT(PIN0, DHT22, 1);
这是一个指向先前调用创建的内存的指针,即DHT dht = ...
DHT *dhtPointer = &dht;
以下是对在第一次调用中创建的内存的引用:
DHT &dhtRef = dht;
您看到语法不同吗?然后,您还会看到您的构造函数没有使用指针,这是您要提出的:
DHT *dht = new DHT(PIN0, DHT22, 1); // pointer
// sensor in the constructor below is just an object, not a pointer, while dht is a pionter
ClimateController(Relay relay, DHT sensor, float minValue, float maxValue);
相反,您需要为继电器和传感器传递一个对象,而不是一个指针。或者您将构造函数更改为采用指针:
ClimateController(Relay *relay, DHT *sensor, float minValue, float maxValue);
在使用中继的情况下,您只需将构造的对象作为const引用传递即可:
ClimateController(const Relay &relay, DHT *sensor, float minValue, float maxValue);
这将允许您像这样通过它:
new ClimateController(Relay(PIN1, false), dht, 0.0, 30.0);
我希望这可以消除您的一些疑问。