0

我有一个名为 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){
4

3 回答 3

1

如果您使用的是 C++11 或更高版本,您应该这样编写代码:

class Output
{
public:
    Output(ushort relayNum, NCD2Relay* ncd2RelayPtr);
    ushort relayNum;
    OutputStatus outputStatus;
    int setOutputOn(void);
    int setOutputOff(void);
    void process(void);
    NCD2Relay* ncd2RelayPtr;
};
class HighWaterDetector {
public:
    HighWaterDetector(Device* device);
    NCD2Relay ncd2Relay;
    // Output output1;
    Output outputs[2];
    CloudMsgParser cloudMsgParser;
    Device * devicePtr;
};

Output::Output(ushort relayNum, NCD2Relay* ncd2RelayPtr2)
    : relayNum(relayNum), ncd2RelayPtr(ncd2RelayPtr2)
{
}
HighWaterDetector::HighWaterDetector(Device* device)
    : ncd2Relay(),
      outputs{Output(1, &ncd2Relay), Output(2, &ncd2Relay)},
      cloudMsgParser(),
      devicePtr(device)
{
}

现场演示:

没有C++11,需要创建默认构造函数

于 2016-08-11T04:37:56.537 回答
0

您的Output类没有默认(无输入)构造函数,只有需要输入值的非平凡构造函数,因此您不能声明固定的Output元素数组。

给出Output一个默认构造函数:

class Output
{
public:
    Output(); // <-- here
    Output(ushort relayNum, NCD2Relay* ncd2RelayPtr);
    ...
};

Output::Output() {
    this->relayNum = 0;
    this->ncd2RelayPtr = 0;
}

或者,您可以为现有的构造函数提供默认参数值,以便它也可以充当默认构造函数:

class Output
{
public:
    Output(ushort relayNum = 0, NCD2Relay* ncd2RelayPtr = 0);
    ...
};

无论哪种方式,您都可以在HighWaterDetector构造函数中执行此操作:

HighWaterDetector::HighWaterDetector(Device* device)
    : devicePtr(device)
{
    outputs[0] = Output(1, &ncd2Relay);
    outputs[1] = Output(2, &ncd2Relay);
}

如果您不喜欢这样,请将您的outputs[]数组更改为 a std::vector

#include <vector>

class HighWaterDetector {
public:
    ...
    std::vector<Output> outputs;
    ...
};

HighWaterDetector::HighWaterDetector(Device* device)
{
    ...
    outputs.reserve(2);
    outputs.push_back(Output(1, &ncd2Relay));
    outputs.push_back(Output(2, &ncd2Relay));
    ...
}
于 2016-08-11T04:27:37.607 回答
0

首先,我认为您需要为Output.

默认构造函数是不带参数的构造函数。这是将为数组中的每个项目调用的内容。

其次,请发布您的构造函数HighWaterDecorator。我怀疑您可以在构造函数中进行初始化ncdRelay,然后将其传递给构造Output数组中的两个对象。

于 2016-08-11T04:17:14.940 回答