如何在构造函数中初始化对象数组?

时间:2016-08-11 04:09:35

标签: c++ constructor compiler-errors compiler-warnings member-initialization

我有一个名为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 Constructor看起来像:

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){

3 个答案:

答案 0 :(得分:0)

首先,我认为你需要Output的默认构造函数。

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

其次,请发布HighWaterDecorator的构造函数。我怀疑你可以在构造函数中初始化ncdRelay,并传递它来构造数组中的两个Output对象。

答案 1 :(得分: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[]数组改为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));
    ...
}

答案 2 :(得分:0)

如果您使用的是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,则需要创建默认构造函数

相关问题