我有一个名为 HighWaterDetector 的课程:
class HighWaterDetector {
public:
HighWaterDetector(Device* device);
NCD2Relay ncd2Relay;
// Output output1;
Output outputs[2];
CloudMsgParser cloudMsgParser;
Device * devicePtr;
};
如何在 HighWaterDetector 的构造函数中初始化“输出”对象数组?
输出类:
class Output
{
public:
Output(ushort relayNum, NCD2Relay* ncd2RelayPtr);
ushort relayNum;
OutputStatus outputStatus;
int setOutputOn(void);
int setOutputOff(void);
void process(void);
NCD2Relay* ncd2RelayPtr;
};
输出构造函数如下所示:
Output::Output(ushort relayNum, NCD2Relay* ncd2RelayPtr2) {
this->relayNum = relayNum;
this->ncd2RelayPtr = ncd2RelayPtr2;
}
我是 C++ 新手,不确定是否可以使 HighWaterDetector 构造函数看起来像:
HighWaterDetector::HighWaterDetector(Device* device){
ncd2Relay = NCD2Relay();
outputs[0] = Output(1, &ncd2Relay);
outputs[1] = Output(2, &ncd2Relay);
cloudMsgParser = CloudMsgParser();
}
出现编译错误:
highWaterDetector.cpp: In constructor 'HighWaterDetector::HighWaterDetector(Device*)':
highWaterDetector.cpp:8:52: error: no matching function for call to 'Output::Output()'
HighWaterDetector::HighWaterDetector(Device* device){
^
highWaterDetector.cpp:8:52: note: candidates are:
In file included from highWaterDetector.h:10:0,
from highWaterDetector.cpp:1:
output.h:20:2: note: Output::Output(ushort, NCD2Relay*)
Output(ushort relayNum, NCD2Relay* ncd2RelayPtr);
^
output.h:20:2: note: candidate expects 2 arguments, 0 provided
output.h:17:7: note: constexpr Output::Output(const Output&)
class Output
^
output.h:17:7: note: candidate expects 1 argument, 0 provided
output.h:17:7: note: constexpr Output::Output(Output&&)
output.h:17:7: note: candidate expects 1 argument, 0 provided
highWaterDetector.cpp: In constructor 'HighWaterDetector::HighWaterDetector(Device*)':
highWaterDetector.cpp:8:52: error: no matching function for call to 'Output::Output()'
HighWaterDetector::HighWaterDetector(Device* device){